2016-11-21 80 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); 
相關問題