2016-11-21 83 views
8

使用Woocommerce 2.6.8,我无法获取docshere on SO中所述的订单项数据信息。Woocommerce - 获取订单项目的价格和数量。

所有我想要的是让行项目的价格和数量,这应该是简单:

$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循环,直到我发现我在寻找关键的粗黑客。

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

回答

16

更新(对于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已被替换,它不再方法但功能一些参数:woocommerce的

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

以前的版本(从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

+0

这工作完全从order对象中获取,也证明了我的大部分原代码也工作正常。问题是我在woocommerce完全加载之前调用了这些方法,所以正确的信息没有被返回!你的代码帮助我意识到别的东西不太对,现在终于解决了,谢谢! – robobobobo

+0

get_item_meta从WC 3.0开始已弃用,应该使用wc_get_order_item_meta代替 – Tofandel

+1

@Tofandel ...正如你可以看到这个答案在WC 3之前一直是女佣。所以我做了一个更新,然后......谢谢。 – LoicTheAztec

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); 
1

一口价可以通过下面的代码

$order->get_item_total($item); 
相关问题