php - Woocommerce get_item() 函数返回 false

标签 php wordpress woocommerce orders

随着 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 …
}

这应该澄清事情。

作为引用:How to get WooCommerce order details

关于php - Woocommerce get_item() 函数返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45433930/

相关文章:

wordpress - 最小化重定向

php - 如何从多行中选择并连接列值?

php - 如果达到购物车商品数量限制,请禁用 WooCommerce 付款方式

php - 从 Woocommerce 电子邮件通知中过滤掉不需要的订单项元数据

wordpress - 如何在自定义页面上按 ID 号显示 Woocommerce 产品价格?

php - 如何在 woocommerce 中获得免费送货的最低订购量

php - 警告 : stream_socket_client(): unable to set private key file

php - Twitter OAUTH - 返回 "0"的响应代码

php - 使用 php 将复选框输入插入 postgresql

html - Wordpress 正在自动调整我的图像大小——如何关闭它?