How Can I Get The Price And Quantity Of An Ordered Item

geekerhub

Updated on:

Get The Price And Quantity

To obtain get the price and quantity of an ordered item, you can gather the necessary information from various sources and channels. Here’s a detailed explanation of how you can acquire the price and quantity details.

  • The custom email body content must be sent in. To retrieve Woo-Commerce order details, we must use the WC_Order object.
  • We can also use the function wc_get_orders() to get the order object and order details based on a parameter.

Woo-Commerce 3.0 is Required.

Use the WC_Order_Item_product and WC_Product Methods in the code instead, such as :


<?php
//Obtaining a WC Order object instance from an ORDER ID.
$order = wc_get_order( $order_id ); 
    
foreach ($order->get_items() as $item_id => $item ) {

    $product = $item->get_product();

    $active_price = $product->get_price(); // The active raw price of the product

    $regular_price  = $product->get_sale_price(); // The wholesale price of the product

    $sale_price = $product->get_regular_price(); // The regular price of the raw commodity

    $product_name = $item->get_name(); // Get the item name (product name)

    $item_quantity = $item->get_quantity(); //Get the amount of the item.

    $item_subtotal = $item->get_subtotal(); // Get the amount in the Subtotals. 

    $item_subto_tax = $item->get_subtotal_tax(); // Get non-discounted item line total tax

    $item_total = $item->get_total(); // Reduce the total of the item line

    $item_total_tax = $item->get_total_tax(); // Reduce the total tax on the item line.

    $item_taxes = $item->get_taxes(); // Get the tax array for each item.

    $item_tax_class = $item->get_tax_class(); // Get the item tax classification.

    $item_tax_status = $item->get_tax_status(); // Determine the item's tax status.

    $item_downloads = $item->get_item_downloads(); // Download the product

    // Displaying this data (to check)
    echo 'Product Title: '.$product_name.' | Quantity: '.$item_quantity.' | Total items: '. number_format( $item_total, 2 );
}
?>

WC_Abstract_order_methods also allow you to access order price and quantity item data with a variety of interesting parameters, such as :

<?php
$order = wc_get_order( $order_id ); // Obtaining a WC Order object instance from an ORDER ID

foreach ($order->get_items() as $item_id => $item){ // Iterating through the order of the "line" items

$inc_tax = true;    // Taxes can be included or excluded.

   // Round at the item level (or not)
$round = false; //The item level is not rounded ("true" for rounding at item level)

$item_cost_excl_disc = $order->get_item_subtotal( $item, $inc_tax, $round );
   //Calculate the cost of an item (not discounted) – this is useful for gateways.

$item_cost_incl_disc = $order->get_item_total( $item, $inc_tax, $round ); //Calculate discounted item cost - important for gateways.

$item_tax_cost       = $order->get_item_tax( $item, $inc_tax, $round ); // Get the cost of an item's tax - important for gateways.

$item_Line_subtotal = $order->get_line_subtotal( $item, $inc_tax, $round ); // Get the line subtotal without the discount.

$item_Line_total     = $order->get_line_total( $item, $inc_tax, $round ); // Get the discounted line total

$item_Line_tax       = $order->get_line_tax( $item ); // Get line tax

$form_line_subtotal  = $order->get_formatted_line_subtotal( $item, $tax_display = '')            // Returns a line subtotal that has been prepared for presentation.
}
?>

wc_data_methods can also be used to retrieve order Item data as an unprotected array, or to retrieve a single nested or custom metadata value from a specified meta key :


<?php
//Obtaining a WC Order object instance from a specified ORDER ID
$order = wc_get_order( $order_id ); 

foreach ($order->get_items() as $item_id => $item ){// Iterating over the order of each   "line" item 
$order_item_data = $item->get_data(); //Get the meta data order item unprotected array.
print_r( $order_item_data ); // display raw data

$item_meta_data = $item->get_meta_data(); //Get order item nested and custom meta info.
print_r( $item_meta_data ); // display raw data

$item_value = $item->get_meta('meta_key'); //Get a value for a given order item's custom or nested meta data.
print_r( $item_value ); //show the raw data (can be a string or an array)
}
?>

The method get_item_meta() has been deprecated and replaced by wc_get_order_item_meta, which is a function with several parameters rather than a method :

  • /**  Summary of Parameters
  •  * @param mixed $item_id 
  • * @param mixed $key
  •  * @param bool $single (default: true)
  •  * @return mixed**/
wc_get_order_item_meta( $item_id, $key, $single = true );
To access the order metadata, utilise the wc_abstract_order function get_item_meta() (the item quantity and the item price total).

As a result, your code will be :

<?php
$order = wc_get_order( $order_id ); // Obtaining the "$order" order object

$order_items = $order->get_items(); // Getting the items in the order

foreach ($order_items as $item_id => $item) {   // Putting the pieces in the right place

$product_name = $item['name'];   // Get the amount of the item.
 
$item_quantity = $order->get_item_meta($item_id, '_qty', true);// Obtain the product's name.    

$item_total = $order->get_item_meta($item_id, '_line_total', true);  // Get the total for each item line.

echo 'Product name: '.$product_name.' | Quantity: '.$item_quantity.' | Item total: '. $item_total;   // Displaying this information (to check)

}
?>

 

2 thoughts on “How Can I Get The Price And Quantity Of An Ordered Item”

  1. Cuando tenga dudas sobre las actividades de sus hijos o la seguridad de sus padres, puede piratear sus teléfonos Android desde su computadora o dispositivo móvil para garantizar su seguridad. Nadie puede monitorear las 24 horas del día, pero existe un software espía profesional que puede monitorear en secreto las actividades de los teléfonos Android sin avisarles.

Leave a Comment