php - Adding Custom Meta Data from Products to Orders items in Woocommerce -
i'm trying custom meta data of woocommerce products order columns in woocommerce admin code wont work on function.php in wordpress theme.
// order meta pd number add_action('woocommerce_add_order_item_meta','adding_custom_data_in_order_items_meta', 1, 3 ); function adding_custom_data_in_order_items_meta( $post_id, $cart_item_key ) { // corresponding product id item: $product_id = $post_id[ 'product_id' ]; //$pd_number = $post_id['_pd_number']; //$pd_number = $_post['_pd_number']; $pd_number = get_post_meta( $post_id[ 'product_id' ], '_pd_number', true ); if ( !empty($pd_number) ) wc_add_order_item_meta($post_id, '_pd_number', $pd_number, true); }
thanks
there errors in code. try instead:
// add the product custom field item meta data in order add_action( 'woocommerce_add_order_item_meta', 'pd_number_order_meta_data', 10, 3 ); function pd_number_order_meta_data( $item_id, $cart_item, $cart_item_key ) { // product custom field value $pd_number = get_post_meta( $cart_item[ 'product_id' ], '_pd_number', true ); // add custom field value order item meta if( ! empty($pd_number) ) wc_update_order_item_meta( $item_id, '_pd_number', $pd_number ); }
code goes in function.php file of active child theme (or theme) or in plugin file.
this should works on woocommerce versions 2.5.x 3+.
Comments
Post a Comment