admin管理员组文章数量:1417580
now with this function i can add new custom order item meta to all item of order.
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta', 10, 2 );
function add_order_item_meta($item_id, $values) {
$key = '_key';
$value = '_value';
wc_update_order_item_meta($item_id, $key, $value);
}
but how can i add different order item meta to each item of order? example i need to add each product price as order_itemmeta.
now with this function i can add new custom order item meta to all item of order.
add_action( 'woocommerce_add_order_item_meta', 'add_order_item_meta', 10, 2 );
function add_order_item_meta($item_id, $values) {
$key = '_key';
$value = '_value';
wc_update_order_item_meta($item_id, $key, $value);
}
but how can i add different order item meta to each item of order? example i need to add each product price as order_itemmeta.
Share Improve this question asked Feb 23, 2019 at 9:11 Hossein Hossein 311 gold badge1 silver badge2 bronze badges1 Answer
Reset to default 5Try,
woocommerce_checkout_create_order_line_item
It has 4 available arguments and available after version WooCommerce 3.3+.
$item
is an instance ofWC_Order_Item_Product
new introduced Class$cart_item_key
is the cart item unique hash key$values
is the cart item$order
an instance of theWC_Order
object (This is a very useful additional argument in some specific cases)
In this hook we will replace the old working functions wc_add_order_item_meta()
by the new WC_Data update_meta_data()
method to be used with $item
argument.
Example:
add_action( 'woocommerce_checkout_create_order_line_item', 'custom_checkout_create_order_line_item', 20, 4 );
function custom_checkout_create_order_line_item( $item, $cart_item_key, $values, $order ) {
// Get a product custom field value
$custom_field_value = get_post_meta( $item->get_product_id(), '_meta_key', true );
// Update order item meta
if ( ! empty( $custom_field_value ) ){
$item->update_meta_data( 'meta_key1', $custom_field_value );
}
// … … Or … …
// Get cart item custom data and update order item meta
if( isset( $values['custom_data'] ) ) {
$item->update_meta_data( 'meta_key2', $values['custom_data'] );
}
}
本文标签: WooCommerce add different order item meta for each item in order
版权声明:本文标题:WooCommerce: add different order item meta for each item in order 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745272627a2650986.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论