Woocommerce 在结帐时获取订单商品 [英] Woocommerce Get order items on checkout
问题描述
我正在尝试在结账购物车时连接到第 3 方应用程序.本质上,我需要传递有关订单的信息,例如订单内的产品.
I am trying to hook into a 3rd party application when a cart is checked out. Essentially i need to pass information about the order such as the products that are inside the order.
我发现的一切都指向我的钩子:woocommerce_new_order
Everything i find points me towards the hook: woocommerce_new_order
当我使用该钩子时,我可以获得有关订单的一些信息,但不是所有信息.
When i use that hook i can get some information about the order but not everything.
add_action('woocommerce_new_order','order_check',10,1);
function order_check($order_id){
echo 'Order id is: '.$order_id;
$order = new WC_Order($order_id);
print_r($order);
echo '-----';
/** CHECK IF order has items */
$order_item = $order->get_items();
print_r($order_item);
exit;
}
例如,上面的代码示例将打印订单数组,但是当它调用 $order->get_items() 时没有返回任何内容.
For instance the above code sample will print the order array but when it call $order->get_items() nothing is returned.
推荐答案
在 woocommerce_new_order
触发时,订单项尚未填充.
At the time woocommerce_new_order
fires, order items is not yet populated.
相反,使用钩子 woocommerce_checkout_order_processed
,然后您会发现所有项目都被填充.
Instead, use the hook woocommerce_checkout_order_processed
and you'll find that all items are then populated.
这篇关于Woocommerce 在结帐时获取订单商品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!