dqjgf0982 2017-08-01 09:24
浏览 33
已采纳

Woocommerce get_item()函数返回false

With WooCommerce 3+ introducing new API to fetch the order and it's details, a lot of things have changed and many things break as well.

Consider the following code in my plugin:

$order = wc_get_order($order_id);
$id= 27;

var_dump($order->get_item($id));

which gives me bool(false). I have checked the database and the order and the item does exist.

Also

var_dump($order) does return the entire order object with all the items.

So basically, only the function get_item does not seem to work.

  • 写回答

1条回答 默认 最新

  • dongqiongzheng0615 2017-08-01 12:12
    关注

    The only explanation is that the ID you are using is not an item_id with a type "line_item"

    I have tried and it works normally as expected using WC_Abstract_Order get_item() method when the item_id is of type "line_item".

    To get and check the correct "line_item" Item IDs from a defined Order ID, try:

    // 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 …
    }
    

    This should clarify things.

    As reference: How to get WooCommerce order details

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题