Adding Custom Meta Data from Products to Orders items in Woocommerce

房东的猫 提交于 2019-12-06 15:38:18

Since Woocommerce 3, a better hook is recommended (see this answer thread).

There is some errors in your code. Try this instead:

// Add the the product custom field as item meta data in the 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 ) {
    // get the product custom field value
    $pd_number = get_post_meta( $cart_item[ 'product_id' ], '_pd_number', true );

    // Add the custom field value to 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 your active child theme (or theme) or also in any plugin file.

This should works on WooCommerce versions from 2.5.x to 3+.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!