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

时间:2016-11-21 00:36:07

标签: php wordpress woocommerce items orders

使用Woocommerce 2.6.8,我无法获得docshere on SO中所述的订单商品数据信息。

我想要的只是获得行项目价格和数量,这应该简单:

$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
   }

仔细观察返回的内容

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:{}}
                    )

            )

    )

)

这都是使用记录的Woocommerce方法,为什么我需要的信息存储在item_meta_array

有谁知道如何获取这些信息?

最好使用文档化的方法,而不是在我找到正在寻找的密钥之前循环遍历item_meta_array

我觉得我必须在这里遗漏一些明显的东西。

3 个答案:

答案 0 :(得分:40)

  

更新(适用于WooCommerce 3 +)

现在,对于代码,您可以使用WC_Order_Item_Product(和WC_Product)方法,例如:

## 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 );
}

此代码经过测试并有效。

  

方法get_item_meta()已被弃用,已被wc_get_order_item_meta取代,它不再是方法,而是具有一些参数的函数

/** 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 );
  

早期版本的woocommerce(从2.4到2.6.x)

您可以使用get_item_meta() WC_Abstract_order方法获取订单元数据(商品数量和商品价格总额)。

所以你的代码将是:

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

此代码经过测试且功能齐全。

参考:Class WC_Abstract_Order Methods

答案 1 :(得分:3)

商品价格可以从order对象获得以下代码

$order->get_item_total( $item );

答案 2 :(得分:1)

请在订购类中查看woocommerce订单项的此文档。 Here

您可以拨打总计获取总订单费用。 如果您想通过 product_id

来检索单项成本
$_product = wc_get_product( $product_id );
$Price = $_product->get_price();

或者你可以这样做。

$price = get_post_meta( get_the_ID(), '_regular_price', true);
$price = get_post_meta( get_the_ID(), '_sale_price', true);
相关问题