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

2022-08-30 16:13:57

使用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

我觉得我一定错过了这里明显的东西。


答案 1

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

此代码经过测试并正常工作。

方法已被弃用,并已被wc_get_order_item_meta替换,它不再是一个方法,而是一个带有一些参数的函数:get_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) {
    // 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方法


答案 2

项目价格可以通过以下代码从对象获得order

$order->get_item_total( $item );

推荐