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 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM