dsg24156 2016-11-21 00:36
浏览 354
已采纳

Woocommerce - 获取订单商品的价格和数量。

Using Woocommerce 2.6.8 , I can't get the Order Item Data information as described in the docs and here on SO.

All I want is to get the Line Item price and Quantity, which should be as simple as:

$order = new WC_Order( $order_id );
$order_items = $order->get_items();
 foreach ($order_items as $items_key => $items_value) {  
           echo $items_value['name']; //this works
           echo $items_value['qty']; //this doesn't work
           echo $items_value[item_meta][_qty][0]; //also doesn't work
           echo $items_value['line_total']; //this doesn't work
   }

Looking closer at what gets returned returned

Array
(
[1] => Array
    (
        [name] => Sample Product 1
        [type] => line_item
        [item_meta] => 
        [item_meta_array] => Array
            (
                [1] => stdClass Object
                    (
                        [key] => _qty
                        [value] => 1
                    )

                [2] => stdClass Object
                    (
                        [key] => _tax_class
                        [value] => 
                    )

                [3] => stdClass Object
                    (
                        [key] => _product_id
                        [value] => 8
                    )

                [4] => stdClass Object
                    (
                        [key] => _variation_id
                        [value] => 0
                    )

                [5] => stdClass Object
                    (
                        [key] => _line_subtotal
                        [value] => 50
                    )

                [6] => stdClass Object
                    (
                        [key] => _line_total
                        [value] => 50
                    )

                [7] => stdClass Object
                    (
                        [key] => _line_subtotal_tax
                        [value] => 0
                    )

                [8] => stdClass Object
                    (
                        [key] => _line_tax
                        [value] => 0
                    )

                [9] => stdClass Object
                    (
                        [key] => _line_tax_data
                        [value] => a:2:{s:5:"total";a:0:{}s:8:"subtotal";a:0:{}}
                    )

            )

    )

)

This is all using documented Woocommerce methods, why is the information I need stored in this item_meta_array?

Does anyone know how I can get that information?

Preferably using documented methods as opposed to a crude hack of looping through the item_meta_array until I find the key I'm looking for.

I feel like I must be missing something obvious here.

  • 写回答

3条回答 默认 最新

  • dongyuhui0418 2016-11-21 08:02
    关注

    Update (For WooCommerce 3+)

    Now for the code you can use WC_Order_Item_Product (and WC_Product) methods instead, like:

    ## For WooCommerce 3+ ##
    
    // Getting an instance of the WC_Order object from a defined ORDER ID
    $order = wc_get_order( $order_id ); 
    
    // Iterating through each "line" items in the order
    foreach ($order->get_items() as $item_id => $item_data) {
    
        // Get an instance of corresponding the WC_Product object
        $product = $item_data->get_product();
        $product_name = $product->get_name(); // Get the product name
    
        $item_quantity = $item_data->get_quantity(); // Get the item quantity
    
        $item_total = $item_data->get_total(); // Get the item line total
    
        // Displaying this data (to check)
        echo 'Product name: '.$product_name.' | Quantity: '.$item_quantity.' | Item total: '. number_format( $item_total, 2 );
    }
    

    This code is tested and works.

    Method get_item_meta() is deprecated and has been replaced by wc_get_order_item_meta and it's not anymore a method but a function with some parameters:

    /** Parameters summary
     * @param mixed $item_id
     * @param mixed $key
     * @param bool $single (default: true)
     * @return mixed
     */
    
    wc_get_order_item_meta( $item_id, $key, $single = true );
    

    Prior versions of woocommerce (from 2.4 to 2.6.x)

    You can use get_item_meta() WC_Abstract_order method, to get the order metadata (the item quantity and the item price total).

    So your code will be:

    // Getting the order object "$order"
    $order = wc_get_order( $order_id );
    // Getting the items in the order
    $order_items = $order->get_items();
    // Iterating through each item in the order
    foreach ($order_items as $item_id => $item_data) {
        // Get the product name
        $product_name = $item_data['name'];
        // Get the item quantity
        $item_quantity = $order->get_item_meta($item_id, '_qty', true);
        // Get the item line total
        $item_total = $order->get_item_meta($item_id, '_line_total', true);
    
        // Displaying this data (to check)
        echo 'Product name: '.$product_name.' | Quantity: '.$item_quantity.' | Item total: '. $item_total;
    }
    

    This code is tested and fully functional.

    Reference: Class WC_Abstract_Order Methods

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 我这模型写的不对吗?为什么lingo解出来的下面影子价格这一溜少一个变量
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波