Regarding Woocommerce. I have custom data that I am adding to the cart. In the functions.php file, I have the following function.
// Display cart item custom data in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_cart_item_custom_on_cart_and_checkout', 10, 2 );
function display_cart_item_custom_on_cart_and_checkout( $cart_item_data, $cart_item ){
if( isset($cart_item['custom_data']['label0']) && isset($cart_item['custom_data']['value0']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label0'],
'value' => $cart_item['custom_data']['value0'],
);
}
if( isset($cart_item['custom_data']['label']) && isset($cart_item['custom_data']['value']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label'],
'value' => $cart_item['custom_data']['value'],
);
}
if( isset($cart_item['custom_data']['label2']) && isset($cart_item['custom_data']['value2']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label2'],
'value' => $cart_item['custom_data']['value2'],
);
}
if( isset($cart_item['custom_data']['label3']) && isset($cart_item['custom_data']['value3']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label3'],
'value' => $cart_item['custom_data']['value3'],
);
}
if( isset($cart_item['custom_data']['label4']) && isset($cart_item['custom_data']['value4']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label4'],
'value' => $cart_item['custom_data']['value4'],
);
}
if( isset($cart_item['custom_data']['label5']) && isset($cart_item['custom_data']['value5']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label5'],
'value' => $cart_item['custom_data']['value5'],
);
}
if( isset($cart_item['custom_data']['label6']) && isset($cart_item['custom_data']['value6']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label6'],
'value' => $cart_item['custom_data']['value6'],
);
}
if( isset($cart_item['custom_data']['label7']) && isset($cart_item['custom_data']['value7']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label7'],
'value' => $cart_item['custom_data']['value7'],
);
}
if( isset($cart_item['custom_data']['label8']) && isset($cart_item['custom_data']['value8']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label8'],
'value' => $cart_item['custom_data']['value8'],
);
}
if( isset($cart_item['custom_data']['label9']) && isset($cart_item['custom_data']['value9']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label9'],
'value' => $cart_item['custom_data']['value9'],
);
}
if( isset($cart_item['custom_data']['label10']) && isset($cart_item['custom_data']['value10']) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label10'],
'value' => $cart_item['custom_data']['value10'],
);
}
return $cart_item_data;
}
This works and the custom data is shown in the cart. However, the custom data does not show on the order and order email. I have seen, on Stackoverflow several answers that provide solutions to this problem but I cannot make them work for my situation. The solutions that I reference are.
Save and display order item custom meta data in Woocommerce
Display and save added custom cart item data on Woocommerce Cart, Checkout and Orders
Can anybody kindly show me what "my" function should be?
Thank you.
First you can optimize and compact your function this way:
// Display cart item custom data in cart and checkout pages
add_filter( 'woocommerce_get_item_data', 'display_cart_item_custom_on_cart_and_checkout', 10, 2 );
function display_cart_item_custom_on_cart_and_checkout( $cart_item_data, $cart_item ){
$keys = array('0','','2','3','4','5','6','7','8','9','10'); // Fields numbers part keys array
// Loop through Fields numbers part keys array
foreach( $keys as $key ) {
if( isset($cart_item['custom_data']['label'.$key]) && isset($cart_item['custom_data']['value'.$key]) ) {
$cart_item_data[] = array(
'name' => $cart_item['custom_data']['label'.$key],
'value' => $cart_item['custom_data']['value'.$key],
);
}
}
return $cart_item_data;
}
Then to save all your custom cart item data as custom order item meta data and display it everywhere on orders and emails, use the following:
// Save cart item custom data as order item meta data and display it everywhere in Orders and email notifications
add_action('woocommerce_checkout_create_order_line_item', 'save_as_custom_order_item_meta_data', 10, 4 );
function save_as_custom_order_item_meta_data( $item, $cart_item_key, $values, $order ) {
$keys = array('0','','2','3','4','5','6','7','8','9','10'); // Fields numbers part keys array()
// Loop through Fields numbers part keys array
foreach( $keys as $key ) {
if( isset( $values['custom_data']['label'.$key] ) && isset( $values['custom_data']['value'.$key] ) ) {
$item->update_meta_data( $values['custom_data']['label'.$key], $values['custom_data']['value'.$key] );
}
}
}
Code goes in functions.php file of the active child theme (or active theme). It should works.