随着 WooCommerce 3+ 引入了新的 API 来获取订单及其详细信息,很多事情都发生了变化,很多事情也发生了变化。
考虑我的插件中的以下代码:
$order = wc_get_order($order_id);
$id= 27;
var_dump($order->get_item($id));
这给了我bool(false)。我已经检查了数据库和订单,并且该项目确实存在。
还有
var_dump($order)
返回包含所有项目的整个订单对象。
所以基本上,只有函数 get_item
似乎不起作用。
最佳答案
The only explanation is that the ID you are using is not an
item_id
with a type"line_item"
…
我已经尝试使用 WC_Abstract_Order
get_item()
方法正常工作,当 item_id
是“line_item”类型。
要从定义的订单 ID 中获取并检查正确的 "line_item"
商品 ID,请尝试:
// define an exiting order ID first
$order_id = 422;
$order = wc_get_order($order_id);
foreach($order->get_items() as $item_id => $item_values){
$item_ids_array[] = $item_id;
}
var_dump( $item_ids_array ); // will output all item IDs (of type "line_item") for this order
## ==> Then now you can try (to check get_item() method):
foreach( $item_ids_array as $item_id ){
var_dump( $order->get_item( $item_id ) ); // Will output each WC_Order_Item_Product Object …
}
这应该澄清事情。
关于php - Woocommerce get_item() 函数返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45433930/