Get Order Items and Wc_Order_Item_Product in Woocommerce 3

Get Order items and WC_Order_Item_Product in WooCommerce 3

If you use the get_id() method, you get your item ID which is 15 in your code.

Get the product ID:

The correct WC_Order_Item_Product method to get the Product Id is: get_product_id()

Get the variation ID:

The correct WC_Order_Item_Product method to get the variation Id is: get_variation_id()

Get the order ID

The correct WC_Order_Item_Product method to get the Order Id is: get_order_id()

Get the WC_Product object

The correct WC_Order_Item_Product method to get WC_Product object is:
get_product()

Get the WC_Order object

The correct WC_Order_Item_Product method to get WC_order object is:
get_order()

Get and unprotecting the data and meta data using WC_Data methods:

  • get_data()
  • get_meta_data()

Get The WC_Product object from the order item ID:

$order_item_id = 15;
$item = new WC_Order_Item_Product($order_item_id);

// The product ID
$product_id = $item->get_product_id();

// The variation ID
$variation_id = $item->get_variation_id();

// The WC_Product object
$product = $item->get_product();

// The quantity
$quantity = $item->get_quantity();

// The order ID
$order_id = $item->get_order_id();

// The WC_Order object
$order = $item->get_order();

// The item ID
$item_id = $item->get_id(); // which is your $order_item_id

// The product name
$product_name = $item->get_name(); // … OR: $product->get_name();

// Get the product SKU (using WC_Product method)
$sku = $product->get_sku();

// Get line item totals (non discounted)
$total = $item->get_subtotal(); // Total without tax (non discounted)
$total_tax = $item->get_subtotal_tax(); // Total tax (non discounted)

// Get line item totals (discounted when a coupon is applied)
$total = $item->get_total(); // Total without tax (discounted)
$total_tax = $item->get_total_tax(); // Total tax (discounted)

Get the order items from the WC_Order object (and use the WC_product Object):

$order_id = 156; // The order_id

// get an instance of the WC_Order object
$order = wc_get_order( $order_id );

// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item ){
//Get the product ID
$product_id = $item->get_product_id();

//Get the variation ID
$variation_id = $item->get_variation_id();

//Get the WC_Product object
$product = $item->get_product();

// The quantity
$quantity = $item->get_quantity();

// The product name
$product_name = $item->get_name(); // … OR: $product->get_name();

//Get the product SKU (using WC_Product method)
$sku = $product->get_sku();

// Get line item totals (non discounted)
$total = $item->get_subtotal(); // Total without tax (non discounted)
$total_tax = $item->get_subtotal_tax(); // Total tax (non discounted)

// Get line item totals (discounted when a coupon is applied)
$total = $item->get_total(); // Total without tax (discounted)
$total_tax = $item->get_total_tax(); // Total tax (discounted)
}

###Accessing data and custom meta data:

1). Unprotecting WC_Order_Item_Product data and custom meta data:

You can use all WC_Order_Item_Product data methods or you can unprotect the data using WC_Data following methods:

$order_id = 156; // The order_id

// get an instance of the WC_Order object
$order = wc_get_order( $order_id );

// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item ){

// Get the common data in an array:
$item_product_data_array = $item->get_data();

// Get the special meta data in an array:
$item_product_meta_data_array = $item->get_meta_data();

// Get the specific meta data from a meta_key:
$meta_value = $item->get_meta( 'custom_meta_key', true );

// Get all additional meta data (formatted in an unprotected array)
$formatted_meta_data = $item->get_formatted_meta_data( ' ', true );


// Get line item totals (non discounted)
$total = $item->get_subtotal(); // Total without tax (non discounted)
$total_tax = $item->get_subtotal_tax(); // Total tax (non discounted)

// Get line item totals (discounted when a coupon is applied)
$total = $item->get_total(); // Total without tax (discounted)
$total_tax = $item->get_total_tax(); // Total tax (discounted)
}

2). The Array Access is still possible (for backwards compatibility with legacy arrays) to get the common data directly:

$order_id = 156; // The order_id

// get an instance of the WC_Order object
$order = wc_get_order( $order_id );

// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item ){


$product_id = $item['product_id']; // Get the product ID
$variation_id = $item['variation_id']; // Get the variation ID

$product_name = $item['name']; // The product name
$item_qty = $item['quantity']; // The quantity

// Get line item totals (non discounted)
$line_total = $item['subtotal']; // or $item['line_subtotal'] -- The line item non discounted total
$line_total_tax = $item['subtotal_tax']; // or $item['line_subtotal_tax'] -- The line item non discounted tax total

// Get line item totals (discounted)
$line_total2 = $item['total']; // or $item['line_total'] -- The line item non discounted total
$line_total_tax2 = $item['total_tax']; // The line item non discounted tax total

// And so on ……
}

As reference:

  • Get the metadata of an order item in woocommerce 3
  • How to get WooCommerce order details

Set some WooCommerce Order and Order item data in a custom array

Updated

You can have many order items in an Order, but as it seem you need to get only the first order item use the following instead:

add_action( 'woocommerce_new_order', 'telegram_notification',  1, 1  );
function telegram_notification( $order_id ) {
$order = wc_get_order( $order_id ); // Get the WC_Order Object

$order_items = $order->get_items(); // Get order items array

$order_item = reset($order_items); // Keep the first order item

$product = $order_item->get_product(); // Get the WC_Product Object

$somemeta = ''; // <== To be defined !!!
$txt = ''; // Initializing

// Add all order related data to our custom indexed array
$arr = array(
'Номер заказа: ' => $order_id,
'Дата: ' => $order->get_date_modified()->date('Y-m-d H:i:s'),
'Название товара: ' => $order_item->get_name(),
'Артикул: ' => $product->get_sku(),
'Мета продукта:' => $somemeta, // ! Not defined
'Цена товара: ' => $order->get_total(),
'Валюта: ' => $order->get_currency(),
'Имя: ' => $order->get_billing_first_name(),
'Фамилия: ' => $order->get_billing_last_name(),
'Телефон: ' => $order->get_billing_phone(),
'Email: ' => $order->get_billing_email(),
'Страна: ' => $order->get_billing_country(),
'Область: ' => $order->get_billing_state(),
'Город: ' => $order->get_billing_city(),
'Адрес1: ' => $order->get_billing_address_1(),
'Адрес2: ' => $order->get_billing_address_2(),
'Индекс: ' => $order->get_billing_postcode(),
'Метод доставки: ' => $order->get_shipping_method(),
'Метод оплаты: ' => $order->get_payment_method_title() // Updated
);

// Loop through the data array to convert it as a string to be sent by SMS
foreach($arr as $key => $value) {
$txt .= "<b>".$key."</b> ".$value."%0A";
};

// Other code to send The SMS from $txt string variable
}

Tested and works…

Related:

  • How to get WooCommerce order details
  • Get Order items and WC_Order_Item_Product in WooCommerce 3

How to get the product sku from order items in Woocommerce

I think you're fiddling around on the actual template page ;-)
In Wordpress we mainly use action hooks to accomplish tasks like this.

Try this, place it in the (child) theme functions.php.

NOTE: only for WooCommerce 3+

add_action( 'woocommerce_thankyou', 'order_created_get_skus', 10 );

function order_created_get_skus($order_id){

$item_sku = array();
$order = wc_get_order( $order_id );

foreach ($order->get_items() as $item) {
$product = wc_get_product($item->get_product_id());
$item_sku[] = $product->get_sku();
}

// now do something with the sku array

}

Regards, Bjorn

Woocommerce - Getting the order item price and quantity.

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 ) {

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

Update: Also all the following WC_Abstract_Order methods allow to get order items data with various interesting options like:

// 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.
}

Thanks to @Casper for his comment


Also WC_Data methods can be used to get order item data as an unprotected array or to get a specific nested or custom meta data value from a specific meta key:

// 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)
}

This code is tested and works.

Method get_item_meta() is deprecated and has been replaced by wc_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) {
// 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;
}

This code is tested and fully functional.

Reference: Class WC_Abstract_Order Methods

Get last product from an order in WooCommerce

To get the latest product from a WC_Order Object, you can use end() function from order items array, like:

$order_items = $order->get_items(); // Get order items
$last_item = end($order_items); // Latest WC_Order_Item_Product Object instance
$product = $last_item->get_product(); // Latest WC_Product Object instance

Related: Get Order items and WC_Order_Item_Product in WooCommerce 3

How to get order items on a foreach loop in WooCommerce 3

You need to use instead the WC_Order method get_items()
as WC_Order get_order_items() method doesn't exist for Woocommerce…

Also since Woocommerce 3 you can use WC_Order_Item_Product get_total() method instead of wc_get_order_item_meta( $item_id, '_line_total', true );

So inside your code you will change the following:

            foreach ( $orders as $order ) {
if ( count( $order->get_items() ) > 0 ) {
foreach ( $order->get_items() as $item_id => $item ) {
// Add order pay to available pay
$available_pay += $item->get_total();
}
}
}

Related threads:

  • Get Order items and WC_Order_Item_Product in Woocommerce 3
  • How to get WooCommerce order details

What is the WooCommerce hook to for purchases details after checkout?

The hook that you are looking at is woocommerce_checkout_create_order_line_item, located in WC_Checkout method create_order_line_items()

The woocommerce_checkout_create_order_line_item action hook allows to adjust line item before save. It has 4 defined arguments:

  • $item is the WC_Order_Item_Product object
  • $cart_item_key is the related cart item key
  • $values is the related cart item
  • $order is the WC_Order Object

All StackOverFlow treads using woocommerce_checkout_create_order_line_item action hook.

Related: Get Order items and WC_Order_Item_Product in WooCommerce 3



Related Topics



Leave a reply



Submit