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

33
使用Woocommerce 2.6.8,我无法像文档这里的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个回答

131

更新 (适用于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 ) {

    // Get an instance of corresponding the WC_Product object
    $product        = $item->get_product();

    $active_price   = $product->get_price(); // The product active raw price

    $regular_price  = $product->get_sale_price(); // The product raw sale price

    $sale_price     = $product->get_regular_price(); // The product raw regular price

    $product_name   = $item->get_name(); // Get the item name (product name)

    $item_quantity  = $item->get_quantity(); // Get the item quantity

    $item_subtotal  = $item->get_subtotal(); // Get the item line total non discounted

    $item_subto_tax = $item->get_subtotal_tax(); // Get the item line total tax non discounted

    $item_total     = $item->get_total(); // Get the item line total discounted

    $item_total_tax = $item->get_total_tax(); // Get the item line total  tax discounted

    $item_taxes     = $item->get_taxes(); // Get the item taxes array

    $item_tax_class = $item->get_tax_class(); // Get the item tax class

    $item_tax_status= $item->get_tax_status(); // Get the item tax status

    $item_downloads = $item->get_item_downloads(); // Get the item downloads

    // Displaying this data (to check)
    echo 'Product name: '.$product_name.' | Quantity: '.$item_quantity.' | Item total: '. number_format( $item_total, 2 );
}

更新:同时 WC_Abstract_Order 方法 还可使用各种有趣选项获取订单商品数据,例如:

// 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) {
     ## Option: Including or excluding Taxes
     $inc_tax = true; 

     ## Option: Round at item level (or not)
     $round   = false; // Not rounded at item level ("true"  for rounding at item level)

     $item_cost_excl_disc = $order->get_item_subtotal( $item, $inc_tax, $round ); // Calculate item cost (not discounted) - useful for gateways.

     $item_cost_incl_disc = $order->get_item_total( $item, $inc_tax, $round ); // Calculate item cost (discounted) - useful for gateways.

     $item_tax_cost       = $order->get_item_tax( $item, $inc_tax, $round ); // Get item tax cost - useful for gateways.

      $item_Line_subtotal = $order->get_line_subtotal( $item, $inc_tax, $round ); // Get line subtotal - not discounted.

     $item_Line_total     = $order->get_line_total( $item, $inc_tax, $round ); // Get line total - discounted

     $item_Line_tax       = $order->get_line_tax( $item ); // Get line tax

     $form_line_subtotal  = $order->get_formatted_line_subtotal( $item, $tax_display = '' ) // Gets line subtotal - formatted for display.
}

感谢 @Casper 的评论


此外,WC_Data 方法可用于获取订单项目数据作为未受保护的数组,或者从特定元键中获取特定嵌套或自定义元数据值:

// 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 ) {
    $order_item_data      = $item->get_data(); // Get WooCommerce order item meta data in an unprotected array
    print_r( $order_item_data ); // display raw data

    $item_meta_data = $item->get_meta_data(); // Get order item nested and custom meta data in an unprotected array
    print_r( $item_meta_data ); // display raw data

    $item_value     = $item->get_meta('meta_key'); // Get specific order item custom or nested meta data value from a meta_key
    print_r( $item_value ); // display raw data (can be a string or an array)
}

这段代码已经测试并且可用。

方法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() 方法来获取订单元数据(条目数量和条目价格总计)。

因此,您的代码将是:

// 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) {
    // Get the product name
    $product_name = $item['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;
}

该代码已经测试并且完全可用。

参考文献:WC_Abstract_Order类方法


1
get_item_meta已经在WC 3.0中被弃用,应改用wc_get_order_item_meta。 - Tofandel
1
@Tofandel...正如您所看到的,这个答案在 WC 3 之前就已经被提出了。因此我进行了更新...谢谢。 - LoicTheAztec
1
@BasvanDijk 但是get_total()方法适用于WC_Order_Item_Product,这是涉及订单“行”项目的主要类,自WooCommerce 3以来可用...请参见:https://docs.woocommerce.com/wc-apidocs/class-WC_Order_Item_Product.html#_get_total - LoicTheAztec
1
@Casper 我已经大幅更新了我的答案...谢谢。 - LoicTheAztec
由于它通过数量进行除法运算,因此以下代码返回“单价”:$order->get_item_total( $item, ... ) - Ciantic
显示剩余5条评论

10

可以通过下面的代码从order对象中获取商品价格

$order->get_item_total( $item );

1
请查看这份文档,了解有关WooCommerce订单类中的行项目的信息。在这里 您可以调用总计来获取订单的总费用。如果您想通过取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);

这将获取产品的今天价格,而不是下订单时的价格。因此,如果使用单价10下订单,然后单价更改,您将获得新价格。订单仍具有旧的“原始”订单价格。


4
对于一个订单项目,这将会破坏与许多插件的兼容性,这些插件可以动态地改变结账/订单价格,使其不同于最初设置的常规和促销价格。为了更好的跨插件兼容性,我认为最好使用 $order_item->get_subtotal()$order_item->get_total() - helgatheviking

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接