Using Woocommerce 2.6.8 , I can't get the Order Item Data information as described in the docs and here on SO.
All I want is to get the Line Item price and Quantity, which should be as simple as:
$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
}
Looking closer at what gets returned returned
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:{}}
)
)
)
)
This is all using documented Woocommerce methods, why is the information I need stored in this item_meta_array
?
Does anyone know how I can get that information?
Preferably using documented methods as opposed to a crude hack of looping through the item_meta_array
until I find the key I'm looking for.
I feel like I must be missing something obvious here.
Update (For WooCommerce 3+)
Now for the code you can use WC_Order_Item_Product
(and WC_Product
) methods instead, like:
## 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 );
}
This code is tested and works.
Method
get_item_meta()
is deprecated and has been replaced bywc_get_order_item_meta
and it's not anymore a method but a function with some parameters:/** 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 );
Prior versions of woocommerce (from 2.4 to 2.6.x)
You can use get_item_meta()
WC_Abstract_order method, to get the order metadata (the item quantity and the item price total).
So your code will be:
// 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;
}
This code is tested and fully functional.
Reference: Class WC_Abstract_Order Methods
Item price can get from order
object by below code
$order->get_item_total( $item );
Please see this documentation for woocommerce Line item in order class. Here
You can call total for get the total order cost. If you want to retrieve the single item cost by taking the product_id
$_product = wc_get_product( $product_id );
$Price = $_product->get_price();
Or you can do this.
$price = get_post_meta( get_the_ID(), '_regular_price', true);
$price = get_post_meta( get_the_ID(), '_sale_price', true);
来源:https://stackoverflow.com/questions/40711160/woocommerce-getting-the-order-item-price-and-quantity