我有以下代码,这些代码根据一些自定义字段创建购物车项目数据:
function parcel_add_shipment_text_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
$service = filter_input( INPUT_POST, 'servicelevel' );
$servicecode = filter_input( INPUT_POST, 'servicecode');
$price = filter_input( INPUT_POST, 'sellprice' );
$chargeweight = filter_input( INPUT_POST, 'chargeweight' );
$cart_item_data['servicelevel'] = $service;
$cart_item_data['servicecode'] = $servicecode;
$cart_item_data['serviceprice'] = $price;
$cart_item_data['chargeweight'] = $chargeweight;
return $cart_item_data; }
add_filter( 'woocommerce_add_cart_item_data', 'parcel_add_shipment_text_to_cart_item', 10, 3 );
然后我在此处添加购物车meta:
function add_custom_field_text_to_order_items( $item, $cart_item_key, $values, $order ) {
$item->add_meta_data( __( 'Service Level', 'Shipment' ), $values['servicelevel'] );
$item->add_meta_data( __( 'Your Reference', 'Shipment' ), $values['shipmentref'] );
$item->add_meta_data( __( 'Sell Price', 'Shipment' ), $values['serviceprice'] );
$item->add_meta_data( __( 'Charge Weight', 'Shipment' ), , $values['chargeweight'] ); }
add_action( 'woocommerce_checkout_create_order_line_item', 'add_custom_field_text_to_order_items', 10, 4 );
如何检索woocommerce_payment_complete或订单完成屏幕中要使用的上述Item元数据?我知道上述数据正在运行,因为我可以在订单屏幕和电子邮件中看到它们。非常感谢您的帮助。
答案 0 :(得分:2)
在woocommerce_payment_complete操作挂钩(或可从其中获取订单ID或订单对象的任何其他挂钩)中使用自定义订单商品元数据:
add_action( 'woocommerce_payment_complete', 'on_action_payment_complete', 10, 1 );
function on_action_payment_complete( $order_id ) {
// Get an instance of the WC_Order Object
$order = wc_get_order( $order_id );
// Loop through order items
foreach( $order->get_items() as $item_id => $item ){
$servicelevel = $item->get_meta('Service Level');
$servicecode = $item->get_meta('Your Reference');
$serviceprice = $item->get_meta('Sell Price');
$chargeweight = $item->get_meta('Charge Weight');
}
}
代码进入您的活动子主题(或活动主题)的function.php文件中。它应该工作。挂钩中的代码已经过测试并且可以正常工作。