Multiple woocommerce custom order checkout fields - php

so, I'm stuck with adding second custom checkout field to my store.
That first custom field is radio buttons and it is added straight in to template field. The other one is the same radio buttons but with different id and names.
Shipping part works just fine, but that Private label is not saving and is not displayed in admin area after order is complete.
Here is what I already have, what am i doing wrong?
/**
* Update the user meta with field value
**/
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
if ($user_id && $_POST['shippinng']) update_user_meta( $user_id, 'shippinng', esc_attr($_POST['shippinng']) );
if ($user_id && $_POST['private']) update_user_meta( $user_id, 'private', esc_attr($_POST['private']) );
}
/**
* Update the order meta with field value
**/
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['shippinng']) update_post_meta( $order_id, 'Transport', esc_attr($_POST['shippinng']));
if ($_POST['private']) update_post_meta( $order_id, 'Private label', esc_attr($_POST['private']));
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Transport').':</strong> ' . get_post_meta( $order->id, 'Transport', true ) . '</p>';
echo '<p><strong>'.__('Private label').':</strong> ' . get_post_meta( $order->id, 'Private label', true ) . '</p>';
}
/**
* Add the field to order emails
**/
add_filter('woocommerce_email_order_meta_keys', 'my_custom_checkout_field_order_meta_keys');
function my_custom_checkout_field_order_meta_keys( $keys ) {
$keys[] = 'Transport';
$keys[] = 'Private label';
return $keys;
}

You have to register private label in order to save it. I advise downloading checkout field editor and examining the code for learning purposes. Comparison has always been a good model to follow from my experience :)

This isn't a full answer but I can see one issue with how you are adding the fields to the emails. It's an old question but I thought I'd answer it to help myself out when I inevitably have to Google it in 6 months.
Doing this:
$keys[] = 'Transport';
$keys[] = 'Private label';
The second line overwrites the first, so the $keys array becomes just array( "Private label" );.
Instead, try formatting it like this:
$keys = array("Transport", "Private label");
This is working on my current project:
/* Add custom field to emails */
add_filter('woocommerce_email_order_meta_keys', 'my_custom_order_meta_keys');
function my_custom_order_meta_keys( $keys ) {
$keys = array("Purchase Order Number", "Preferred Delivery Date"); // This will look for both of the specified fields and add them to the emails
return $keys;
}

Related

Woocommerce Product Export : add multiple meta fields using function add_export_data( $value, $product )

Wordpress Site : Woocommerce Plugin
I have multiple meta product fields that I need to export.
I have looked up the reference in WooCommerce Product CSV Importer & Exporter Documentation but I am having a little trouble understanding how to expand the example to add more than one custom column.
This part I am fine with
$columns is an array
So all I need to do is add an array item for each custom field.
/**
* Add the custom column to the exporter and the exporter column menu.
*
* #param array $columns
* #return array $columns
*/
function add_export_column( $columns ) {
// column slug => column name
$columns['my_custom_supplier_product_code'] = 'Supplier Product CODE';
$columns['my_custom_supplier_product_desc'] = 'Supplier Product DESC';
return $columns;
}
add_filter( 'woocommerce_product_export_column_names', 'add_export_column' );
add_filter( 'woocommerce_product_export_product_default_columns', 'add_export_column' );
Here I am not quite sure what to do
REFERENCE
/**
* Provide the data to be exported for one item in the column.
*
* #param mixed $value (default: '')
* #param WC_Product $product
* #return mixed $value - Should be in a format that can be output into a text file (string, numeric, etc).
*/
function add_export_data( $value, $product ) {
$value = $product->get_meta( 'custom_column', true, 'edit' );
return $value;
}
// Filter you want to hook into will be: 'woocommerce_product_export_product_column_{$column_slug}'.
add_filter( 'woocommerce_product_export_product_column_custom_column', 'add_export_data', 10, 2 );
My mod for my first custom column
/**
* Provide the data to be exported for one item in the column.
*
* #param mixed $value (default: '')
* #param WC_Product $product
* #return mixed $value - Should be in a format that can be output into a text file (string, numeric, etc).
*/
function add_export_data( $value, $product ) {
$value = $product->get_meta( 'my_custom_supplier_product_code', true, 'edit' );
return $value;
}
// Filter you want to hook into will be: 'woocommerce_product_export_product_column_{$column_slug}'.
add_filter( 'woocommerce_product_export_product_column_my_custom_supplier_product_code', 'add_export_data', 10, 2 );
Now how do I add what is required for my second column ?
Should I have
2 x add_export_data functions
2 x add_filter
/**
* Provide the data to be exported for one item in CUSTOM COLUMN 1
*
* #param mixed $value (default: '')
* #param WC_Product $product
* #return mixed $value - Should be in a format that can be output into a text file (string, numeric, etc).
*/
function add_export_data_my_custom_supplier_product_code( $value, $product ) {
$value = $product->get_meta( 'my_custom_supplier_product_code', true, 'edit' );
return $value;
}
// Filter you want to hook into will be: 'woocommerce_product_export_product_column_{$column_slug}'.
add_filter( 'woocommerce_product_export_product_column_my_custom_supplier_product_code', 'add_export_data_my_custom_supplier_product_code', 10, 2 );
/**
* Provide the data to be exported for one item in CUSTOM COLUMN 2
*
* #param mixed $value (default: '')
* #param WC_Product $product
* #return mixed $value - Should be in a format that can be output into a text file (string, numeric, etc).
*/
function add_export_data_my_custom_supplier_product_desc( $value, $product ) {
$value = $product->get_meta( 'my_custom_supplier_product_desc', true, 'edit' );
return $value;
}
// Filter you want to hook into will be: 'woocommerce_product_export_product_column_{$column_slug}'.
add_filter( 'woocommerce_product_export_product_column_my_custom_supplier_product_desc', 'add_export_data_my_custom_supplier_product_desc', 10, 2 );
Note that I have tried the code as above, but for some reason
ALL of my columns appear in the spreadsheet
SOME are empty and are not pulling the data from the Product Custom Fields
Rather than 'hacking around' trying to fix the issue, I am now stepping back to try to get a better understanding of how this works.
Thanks
Answering my own question here, and a suggestion that may help others.
Yes, I was correct in my code snippet above.
It does work as expected.
#CBroe was correct:
Maybe start by double-checking the spellings everywhere. The name of
the hooks seem to be related directly to the column slugs, so even the
tiniest misspelling there will likely cause it to not call any
function for the field in question.
My issues were essentially finger trouble
I have 11 Custom Fields.
That gets a little unwieldy when the custom_field_slug references get separated by other code.
So I am adding here
a breakdown to explain what steps are needed
and a formatting suggestion that has made life a lot easier for me to scan across a whole group of related items to make sure I haven't got any typos
STEP 1 : Add Custom Columns to Array for Mapping
/* [ mycode ] EXPORT : ADD custom columns to Exporter
------------------------------------------------------------------------------*/
/**
* Add the custom columns to the exporter and the exporter column menu.
*
* #param array $columns
* #return array $columns
*
*/
function add_export_columns( $columns ) {
// ---------------------------------------------------- Company SYSTEM Fields
// column slug > column name
$columns['company_system_item_code'] = 'Company SYSTEM Item Code';
$columns['company_system_item_description'] = 'Company SYSTEM Item Description';
$columns['company_system_item_category'] = 'Company SYSTEM Item Category';
$columns['company_system_item_unit_weight'] = 'Company SYSTEM Item Unit Weight';
$columns['company_system_item_unit_price'] = 'Company SYSTEM Item Unit Price';
// ---------------------------------------------------- Company WEBSITE Fields
// column slug > column name
$columns['company_website_discount_percent'] = 'Company WEBSITE Discount Percent';
$columns['company_website_item_unit_price'] = 'Company WEBSITE Item Unit Price';
$columns['company_website_item_minimum_units'] = 'Company WEBSITE Item Minimum Units';
// ---------------------------------------------------- Company SUPPLIER Fields
// column slug > column name
$columns['company_supplier_item_code'] = 'Company_SUPPLIER Item Code';
$columns['company_supplier_item_description'] = 'Company SUPPLIER Item Description';
$columns['company_supplier_item_unit_price'] = 'Company SUPPLIER Item Unit Price';
return $columns;
}
add_filter( 'woocommerce_product_export_column_names', 'add_export_columns' );
add_filter( 'woocommerce_product_export_product_default_columns', 'add_export_columns' );
STEP 2 : All the Export Data Functions
/* [ mycode ] EXPORT : GET data to be exported
------------------------------------------------------------------------------*/
/**
*
* Functions to provide the data to be exported for one item in EACH column.
*
*/
// ------------------------------------------------------- Company SYSTEM Fields
function add_export_data_company_system_item_code( $value, $product ) {
$value = $product->get_meta( 'company_system_item_code', true, 'edit' );
return $value;
}
function add_export_data_company_system_item_description( $value, $product ) {
$value = $product->get_meta( 'company_system_item_description', true, 'edit' );
return $value;
}
function add_export_data_company_system_item_category( $value, $product ) {
$value = $product->get_meta( 'company_system_item_category', true, 'edit' );
return $value;
}
function add_export_data_company_system_item_unit_weight( $value, $product ) {
$value = $product->get_meta( 'company_system_item_unit_weight', true, 'edit' );
return $value;
}
function add_export_data_company_system_item_unit_price( $value, $product ) {
$value = $product->get_meta( 'company_system_item_unit_price', true, 'edit' );
return $value;
}
// ------------------------------------------------------- Company WEBSITE Fields
function add_export_data_company_website_discount_percent( $value, $product ) {
$value = $product->get_meta( 'company_website_discount_percent', true, 'edit' );
return $value;
}
function add_export_data_company_website_item_unit_price( $value, $product ) {
$value = $product->get_meta( 'company_website_item_unit_price', true, 'edit' );
return $value;
}
function add_export_data_company_website_item_minimum_units( $value, $product ) {
$value = $product->get_meta( 'company_website_item_minimum_units', true, 'edit' );
return $value;
}
// ------------------------------------------------------- Company SUPPLIER Fields
function add_export_data_company_supplier_item_code( $value, $product ) {
$value = $product->get_meta( 'company_supplier_item_code', true, 'edit' );
return $value;
}
function add_export_data_company_supplier_item_description( $value, $product ) {
$value = $product->get_meta( 'company_supplier_item_description', true, 'edit' );
return $value;
}
function add_export_data_company_supplier_item_unit_price( $value, $product ) {
$value = $product->get_meta( 'company_supplier_item_unit_price', true, 'edit' );
return $value;
}
STEP 3 : All the Filters
/**
*
* Add filters to hook in for EACH column
*
*/
// ------------------------------------------------------- Company SYSTEM Fields
add_filter( 'woocommerce_product_export_product_column_company_system_item_code' , 'add_export_data_company_system_item_code', 10, 2 );
add_filter( 'woocommerce_product_export_product_column_company_system_item_description' , 'add_export_data_company_system_item_description', 10, 2 );
add_filter( 'woocommerce_product_export_product_column_company_system_item_category' , 'add_export_data_company_system_item_category', 10, 2 );
add_filter( 'woocommerce_product_export_product_column_company_system_item_unit_weight' , 'add_export_data_company_system_item_unit_weight', 10, 2 );
add_filter( 'woocommerce_product_export_product_column_company_system_item_unit_price' , 'add_export_data_company_system_item_unit_price', 10, 2 );
// ------------------------------------------------------- Company WEBSITE Fields
add_filter( 'woocommerce_product_export_product_column_company_website_discount_percent' , 'add_export_data_company_website_discount_percent', 10, 2 );
add_filter( 'woocommerce_product_export_product_column_company_website_item_unit_price' , 'add_export_data_company_website_item_unit_price', 10, 2 );
add_filter( 'woocommerce_product_export_product_column_company_website_item_minimum_units', 'add_export_data_company_website_item_minimum_units', 10, 2 );
// ------------------------------------------------------- Company SUPPLIER Fields
add_filter( 'woocommerce_product_export_product_column_company_supplier_item_code' , 'add_export_data_company_supplier_item_code', 10, 2 );
add_filter( 'woocommerce_product_export_product_column_company_supplier_item_description' , 'add_export_data_company_supplier_item_description', 10, 2 );
add_filter( 'woocommerce_product_export_product_column_company_supplier_item_unit_price' , 'add_export_data_company_supplier_item_unit_price', 10, 2 );
NOTE also the white space padding I used to align the components.
When you have many column slugs of different length, it's quite awkward to scan for errors.
If you don't have every hook and every function named correctly with the appropriate column slugs, then you won't get the expected results.
Hopefully this worked example may be useful for someone else in future.

Add a custom field as a default value in WooCommerce backend and populate previous orders

I know that the first part of my question is possible but haven't found how to add a custom field to all orders in the back end and then populate it with a default value.
I'm looking to create a custom field called "Merchant Identifier" and then populate that with a default name e.g "Company X".
I looked at this code which adds an input value at the checkout and then shows in an order summary, but I only need a field adding as a custom field to every order in the back end.
/**
* Process the checkout
*/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
// Check if set, if its not set add an error.
if ( ! $_POST['billing_phone_new'] )
wc_add_notice( __( 'Phone 2 is compulsory. Please enter a value' ), 'error' );
}
/**
* Update the order meta with field value
*/
add_action( 'woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta' );
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ( ! empty( $_POST['billing_phone_new'] ) ) {
update_post_meta( $order_id, 'billing_phone_new', sanitize_text_field( $_POST['billing_phone_new'] ) );
}
}
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta($order){
echo '<p><strong>'.__('Phone 2').':</strong> <br/>' . get_post_meta( $order->get_id(), 'billing_phone_new', true ) . '</p>';
}
Once that custom field appears on all new and old orders I can then add this add this as a column in a scheduled CSV export (that's for later - I just need to achieve the first part).
I'm not sure whether I need to have a hidden field in the checkout first with a default value OR whether I can just add a custom field that shows on all the orders in the back end using a different method.
Anyone able to help?
Thanks
For new orders you can use the following
// Update the order meta with value
function action_woocommerce_checkout_update_order_meta( $order_id ) {
// Meta value
$meta_value = 'Company X';
update_post_meta( $order_id, 'merchant_identifier', $meta_value );
}
add_action( 'woocommerce_checkout_update_order_meta', 'action_woocommerce_checkout_update_order_meta', 10, 1 );
// OPTIONAL (will still work without this code, this is just to show it visually)
// Display field value on the order edit page
function action_woocommerce_admin_order_data_after_billing_address( $order ) {
echo '<p><strong>' . __( 'Merchant Identifier', 'woocommerce') . ':</strong> ' . $order->get_meta( 'merchant_identifier' ) . '</p>';
}
add_action( 'woocommerce_admin_order_data_after_billing_address', 'action_woocommerce_admin_order_data_after_billing_address', 10, 1 );
For existing orders you can perform the following function, after it has been executed (view any page - frontend) it may be removed.
// Run once, delete afterwards
function set_meta_for_old_orders () {
// Get ALL orders where meta key not exists
$orders = wc_get_orders( array(
'limit' => -1, // Query all orders
'meta_key' => 'merchant_identifier', // Post meta_key
'meta_compare' => 'NOT EXISTS', // Comparison argument
));
if ( ! empty ( $orders ) ) {
// Meta value
$meta_value = 'Company X';
foreach ( $orders as $order ) {
$order->update_meta_data( 'merchant_identifier', $meta_value );
$order->save();
}
echo 'Done!';
}
}
// Call function
add_action( 'wp_footer', 'set_meta_for_old_orders' );

Import & Export product Brands in Woocommerce 3.x

Since WooCommerce 3.x there is native Brand support now. However, Brands are not part of the Default Import/Export functionality of a Product. I found the documentation on how to add a custom column in import/export:
/**
* Add the custom column to the exporter and the exporter column menu.
*
* #param array $columns
* #return array $columns
*/
function add_export_column( $columns ) {
// column slug => column name
$columns['custom_column'] = 'Custom Column';
return $columns;
}
add_filter( 'woocommerce_product_export_column_names', 'add_export_column' );
add_filter( 'woocommerce_product_export_product_default_columns', 'add_export_column' );
/**
* Provide the data to be exported for one item in the column.
*
* #param mixed $value (default: '')
* #param WC_Product $product
* #return mixed $value - Should be in a format that can be output into a text file (string, numeric, etc).
*/
function add_export_data( $value, $product ) {
$value = $product->get_meta( 'custom_column', true, 'edit' );
return $value;
}
// Filter you want to hook into will be: 'woocommerce_product_export_product_column_{$column_slug}'.
add_filter( 'woocommerce_product_export_product_column_custom_column', 'add_export_data', 10, 2 );
Using $product->get_meta( 'brands', true, 'edit' ); did not work out. How can I add Brands to the import/export?
UPDATE:
What I did to fix this problem.. - Moved Brands to "Tags" Column - On Admin, Bulk Edit Products by tag, and mark the corresponding Column.
I've been using the Ultimate Brands plugin & thought too there must be a way of updating the custom taxonomy values via CSV. I managed to import brand names using the Woocommerce Product CSV Import Suite but have yet to find a way to export via the Woocommerce built in Exporter. Not being a developer, I thought it must be something related to the 'get_terms' function. I've tried many variations with no luck as yet. Was thinking it should look something like this, but not managed to get it to work yet:
function add_export_data() {
$product_id = $post->ID;
$value = get_terms($product_id, 'product_brand');
return $value;
}
Filter you want to hook into will be: 'woocommerce_product_export_product_column_{$column_slug}'.
So if your custom column name is product_brand, then add_filter will be like:
add_filter( 'woocommerce_product_export_product_column_product_brand', 'add_export_data', 10, 2 );
function add_export_data( $value, $product ) {
$value = $product->get_meta( 'product_brand', true, 'edit' );
return $value;
}
If your custom column name is brands, then add_filter will be like:
add_filter( 'woocommerce_product_export_product_column_brands', 'add_export_data', 10, 2 );
function add_export_data( $value, $product ) {
$value = $product->get_meta( 'brands', true, 'edit' );
return $value;
}

Add custom field data to WooCommerce order

I have a custom field on my WooCommerce single product. It sends to the cart fine, it displays on checkout fine, it shows in the order in the dashboard fine.
What I am now trying to do is set the value as a custom field in the order page so I am able to amend the text when I need to. For some reason when I submit the form this step isn't working.
The code that i use in my functions.phpfile:
// Add the field to the product
add_action('woocommerce_before_add_to_cart_button', 'my_custom_checkout_field');
function my_custom_checkout_field() {
echo '<div id="my_custom_checkout_field"><h3>'.__('My Field').'</h3>';
echo '<label>fill in this field</label> <input type="text" name="my_field_name">';
echo '</div>';
}
// Store custom field
function save_my_custom_checkout_field( $cart_item_data, $product_id ) {
if( isset( $_REQUEST['my_field_name'] ) ) {
$cart_item_data[ 'my_field_name' ] = $_REQUEST['my_field_name'];
/* below statement make sure every add to cart action as unique line item */
$cart_item_data['unique_key'] = md5( microtime().rand() );
}
return $cart_item_data;
}
add_action( 'woocommerce_add_cart_item_data', 'save_my_custom_checkout_field', 10, 2 );
// Render meta on cart and checkout
function render_meta_on_cart_and_checkout( $cart_data, $cart_item = null ) {
$custom_items = array();
/* Woo 2.4.2 updates */
if( !empty( $cart_data ) ) {
$custom_items = $cart_data;
}
if( isset( $cart_item['my_field_name'] ) ) {
$custom_items[] = array( "name" => 'My Field', "value" => $cart_item['my_field_name'] );
}
return $custom_items;
}
add_filter( 'woocommerce_get_item_data', 'render_meta_on_cart_and_checkout', 10, 2 );
// Display as order meta
function my_field_order_meta_handler( $item_id, $values, $cart_item_key ) {
if( isset( $values['my_field_name'] ) ) {
wc_add_order_item_meta( $item_id, "my_field_name", $values['my_field_name'] );
}
}
add_action( 'woocommerce_add_order_item_meta', 'my_field_order_meta_handler', 1, 3 );
/** THIS IS WHERE I'M STUCK **/
add_action('woocommerce_checkout_process', 'my_custom_checkout_field_process');
function my_custom_checkout_field_process() {
global $woocommerce;
// Check if set, if its not set add an error. This one is only requite for companies
if ($_POST['billing_company'])
if (!$_POST['my_field_name'])
$woocommerce->add_error( __('Please enter your XXX.') );
}
// Update the user meta with field value
add_action('woocommerce_checkout_update_user_meta', 'my_custom_checkout_field_update_user_meta');
function my_custom_checkout_field_update_user_meta( $user_id ) {
if ($user_id && $_POST['my_field_name']) update_user_meta( $user_id, 'my_field_name', esc_attr($_POST['my_field_name']) );
}
// Update the order meta with field value
add_action('woocommerce_checkout_update_order_meta', 'my_custom_checkout_field_update_order_meta');
function my_custom_checkout_field_update_order_meta( $order_id ) {
if ($_POST['my_field_name']) update_post_meta( $order_id, 'My Field', esc_attr($_POST['my_field_name']));
}
Screenshot of what currently happens:
What I would like to happen:
Any help would be greatly appreciated.
Updated: compatibility with Woocommerce version 3+
You have missing the function to display this custom field value on the order edit page:
/**
* Display field value on the order edit page
*/
add_action( 'woocommerce_admin_order_data_after_billing_address', 'my_custom_checkout_field_display_admin_order_meta', 10, 1 );
function my_custom_checkout_field_display_admin_order_meta( $order ){
$order_id = method_exists( $order, 'get_id' ) ? $order->get_id() : $order->id;
echo '<p><strong>'.__('My Field Name').':</strong> ' . get_post_meta( $order_id, 'my_field_name', true ) . '</p>';
}
On the reference link below, you have all original wooThemes functional working code snippets. It's an excellent fully functional tutorial.
Reference: [Customizing checkout fields using actions and filters][1]
Edit: Get a custom label displayed with your custom field value in Order item meta
To get a custom label like "MY field name" with your custom field value (in order items meta) instead of a slug like my_field_name, refer to this treads:
Saving a product custom field and displaying it in cart page
Displaying product custom fields values in the order once processed
Adding user custom field value to order items details
I don't know if this still is relevant, I have tried to do this with code, unfortunately, I got stuck in the way. I have tried this woocommerce checkout field editor plugin, which did well to add custom field data to woocomemrce order.

Woocommerce Admin Order Details - Show custom data on order details page

I'm searching and trying it for 2 days with no success, please help.
I want to filter woocommerce orders to add additional details from db to order details page based on product attribute but I can't find the right woocommerce action/filter hook for this task.
Here suppose I've variable $is_customized = false;
If $is_customized == true then I need to add custom data from database to orders detail page.
NOTE: I don't want to add additional meta box instead I want to change order detail table for:
Replacing the default Product image with the image stored in database
and,
Adding a div containing custom attributes below product name.
I've all these values in my variables but I can't figure out which action hook should I use.
I've attached an image for clarification.
Just need to know if I can change / filter these order results and how ?
I appreciate for your time and help.
Thanks
Here's a start on how to display some extra data on the woocommerce_before_order_itemmeta hook:
add_action( 'woocommerce_before_order_itemmeta', 'so_32457241_before_order_itemmeta', 10, 3 );
function so_32457241_before_order_itemmeta( $item_id, $item, $_product ){
echo '<p>bacon</p>';
}
I don't know how you are saving your data, so I can't make more a more precise suggestion. Keep in mind that immediately following that hook, anything you've saved as order item meta will automatically display.
Filtering the image is more difficult. I've found this gist as a start, but it requires some custom conditional logic as you don't want to filter the thumbnail everywhere, but only in orders.
Edit: Currently the best I can do for filtering the item thumbnails:
add_filter( 'get_post_metadata', 'so_32457241_order_thumbnail', 10, 4 );
function so_32457241_order_thumbnail( $value, $post_id, $meta_key, $single ) {
// We want to pass the actual _thumbnail_id into the filter, so requires recursion
static $is_recursing = false;
// Only filter if we're not recursing and if it is a post thumbnail ID
if ( ! $is_recursing && $meta_key === '_thumbnail_id' ) {
$is_recursing = true; // prevent this conditional when get_post_thumbnail_id() is called
$value = get_post_thumbnail_id( $post_id );
$is_recursing = false;
$value = apply_filters( 'post_thumbnail_id', $value, $post_id ); // yay!
if ( ! $single ) {
$value = array( $value );
}
}
return $value;
}
add_filter( 'post_thumbnail_id', 'so_custom_order_item_thumbnail', 10, 2 );
function so_custom_order_item_thumbnail( $id, $post_id ){
if( is_admin() ){
$screen = get_current_screen();
if( $screen->base == 'post' && $screen->post_type == 'shop_order' ){
// this gets you the shop_order $post object
global $post;
// no really *good* way to check post item, but could possibly save
// some kind of array in the order meta
$id = 68;
}
}
return $id;
}

Categories