2017-07-28 96 views
1

我試圖將我的woocommerce產品的自定義元數據傳遞到我的Woocommerce Admin中的我的訂單列中,但此代碼不會在我的function.php工作在WordPress主題中。從產品添加自定義元數據以訂單Woocommerce中的項目

// Order Get Meta for 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) { 

    // The corresponding Product Id for the 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); 
}

感謝

回答

1

。在你的代碼中的一些錯誤。試試這個:

// 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); 
} 

代碼放在您的活動子主題(或主題)的function.php文件或也以任何插件文件。

這應該適用於從2.5.x到3+的WooCommerce版本。

相關問題