我正在为WooCommerce订单创建克隆功能. 我想要的是确保产品项元数据被正确克隆.
/**
* Get the original order items to be clone
* Each product inside the new order must also contain the meta_data
*/
$order = wc_get_order($post_id);
$new_order = wc_get_order($new_post_id);
foreach ($order->get_items() as $item_id => $item) {
$product_id = $item->get_product_id();
$quantity = $item->get_quantity();
// Get product's meta data values
$_wwp_wholesale_priced = get_post_meta( $product_id, '_wwp_wholesale_priced', true);
$_wwp_wholesale_role = get_post_meta( $product_id, '_wwp_wholesale_role', true);
$_amount_requested = get_post_meta( $product_id, '_amount_requested', true);
$_amount_colli = get_post_meta( $product_id, '_amount_colli', true);
$args = array(
'_wwp_wholesale_priced' => $_wwp_wholesale_priced,
'_wwp_wholesale_role' => $_wwp_wholesale_role,
'_amount_requested' => $_amount_requested,
'_amount_colli' => $_amount_colli,
);
$new_order->add_product(wc_get_product($product_id), $quantity, $args);
}
在文档中,看起来传递$args
应该可以做到这一点,但我没有看到传递任何meta.我也试过使用$item->add_meta_data
和$item->update_meta_data
,但它们似乎不起作用.我遗漏了什么?