When developing a WooCommerce store, you may need to retrieve the price, quantity, and other details of products purchased by a customer. This information is useful when customizing order emails, displaying order summaries, generating reports, or adding custom functionality to the checkout process.
WooCommerce provides order and order item methods that make it easy to retrieve this information. You can use the WC_Order and WC_Order_Item_Product classes to access product names, quantities, line totals, taxes, and other order details.
In this tutorial, you will learn how to get the price and quantity of ordered products using WooCommerce PHP functions.
1. Get the WooCommerce Order Object
First, you need to retrieve the order object using its order ID. WooCommerce provides the wc_get_order() function for this purpose.
$order_id = 123;
$order = wc_get_order( $order_id );
if ( $order ) {
// Order details are available here.
}
Replace 123 with the actual WooCommerce order ID. Always check whether the order object was returned before accessing its methods.
2. Get the Price and Quantity of Each Ordered Product
You can retrieve all product line items from an order using the get_items() method. Then, loop through the items to access their names, quantities, and prices.
Use the following code:
$order_id = 123;
$order = wc_get_order( $order_id );
if ( $order ) {
foreach ( $order->get_items() as $item_id => $item ) {
$product_name = $item->get_name();
$item_quantity = $item->get_quantity();
// Line subtotal before discounts, excluding tax.
$item_subtotal = $item->get_subtotal();
// Line total after discounts, excluding tax.
$item_total = $item->get_total();
// Tax amount for the line item.
$item_total_tax = $item->get_total_tax();
echo 'Product: ' . esc_html( $product_name );
echo ' | Quantity: ' . esc_html( $item_quantity );
echo ' | Line Total: ' . wp_kses_post(
wc_price( $item_total, array( 'currency' => $order->get_currency() ) )
);
echo '<br>';
}
}
This example retrieves the following information:
- Product name: The name saved in the order item.
- Quantity: The number of units purchased.
- Item subtotal: The line amount before discounts, excluding tax.
- Item total: The line amount after discounts, excluding tax.
- Item total tax: The tax amount calculated for the line item.
These values come from the order item rather than the current product record. This distinction is important because the product price may change after the customer places an order.
3. Get the Current Product Price and Regular or Sale Price
You can also retrieve the product object associated with an order item. This allows you to access the product’s current price information.
$order_id = 123;
$order = wc_get_order( $order_id );
if ( $order ) {
foreach ( $order->get_items() as $item ) {
$product = $item->get_product();
if ( ! $product ) {
continue;
}
$active_price = $product->get_price();
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
echo 'Product: ' . esc_html( $item->get_name() );
echo ' | Current Price: ' . esc_html( $active_price );
echo ' | Regular Price: ' . esc_html( $regular_price );
echo ' | Sale Price: ' . esc_html( $sale_price );
echo '<br>';
}
}
Important: These methods retrieve the product’s current prices, not necessarily the price paid when the order was placed. If you need the actual amount charged to the customer, use the order item methods such as get_subtotal() and get_total().
4. Get Order Item Subtotal, Total, and Tax
WooCommerce provides several methods for retrieving the financial details of an order item. These methods are useful when you need to calculate discounts, taxes, or the final amount paid for a product.
$order_id = 123;
$order = wc_get_order( $order_id );
if ( $order ) {
foreach ( $order->get_items() as $item ) {
$item_quantity = $item->get_quantity();
// Line subtotal before discounts, excluding tax.
$item_subtotal = $item->get_subtotal();
// Line subtotal tax.
$item_subtotal_tax = $item->get_subtotal_tax();
// Line total after discounts, excluding tax.
$item_total = $item->get_total();
// Tax on the discounted line total.
$item_total_tax = $item->get_total_tax();
// Tax details.
$item_taxes = $item->get_taxes();
echo 'Quantity: ' . esc_html( $item_quantity );
echo ' | Subtotal: ' . esc_html( $item_subtotal );
echo ' | Total: ' . esc_html( $item_total );
echo ' | Tax: ' . esc_html( $item_total_tax );
echo '<br>';
}
}
Here is what the main methods return:
get_quantity(): Returns the ordered quantity.get_subtotal(): Returns the line subtotal before discounts, excluding tax.get_subtotal_tax(): Returns the tax on the line subtotal.get_total(): Returns the line total after discounts, excluding tax.get_total_tax(): Returns the tax on the discounted line total.get_taxes(): Returns the tax data associated with the order item.get_tax_class(): Returns the item’s tax class.get_tax_status(): Returns the item’s tax status.get_name(): Returns the product name saved in the order.
5. Get the Per-Unit Price from an Order Item
If you need the price per unit rather than the total for the entire order line, you can divide the line subtotal or line total by the ordered quantity.
$order_id = 123;
$order = wc_get_order( $order_id );
if ( $order ) {
foreach ( $order->get_items() as $item ) {
$quantity = (int) $item->get_quantity();
if ( $quantity <= 0 ) {
continue;
}
$subtotal_per_unit = $item->get_subtotal() / $quantity;
$total_per_unit = $item->get_total() / $quantity;
echo 'Product: ' . esc_html( $item->get_name() );
echo ' | Price Before Discount: ' . esc_html( wc_format_decimal( $subtotal_per_unit, 2 ) );
echo ' | Price After Discount: ' . esc_html( wc_format_decimal( $total_per_unit, 2 ) );
echo '<br>';
}
}
The first value represents the per-unit price before discounts, while the second represents the per-unit price after discounts. Both values exclude tax.
6. Get Order Item Data and Custom Metadata
WooCommerce order items can contain additional information, including variation attributes and custom metadata added by plugins or custom code.
You can retrieve the order item’s data using get_data():
$order_id = 123;
$order = wc_get_order( $order_id );
if ( $order ) {
foreach ( $order->get_items() as $item ) {
$item_data = $item->get_data();
echo '<pre>';
print_r( $item_data );
echo '</pre>';
}
}
If you only need a particular custom metadata value, use the get_meta() method:
$order_id = 123;
$order = wc_get_order( $order_id );
if ( $order ) {
foreach ( $order->get_items() as $item ) {
$custom_value = $item->get_meta( 'meta_key', true );
if ( '' !== $custom_value ) {
echo esc_html( $custom_value );
}
}
}
Replace meta_key with the actual metadata key stored on the order item.
You can also retrieve the item’s metadata objects using get_meta_data() when you need to inspect all its metadata.
7. Use WC_Order Methods to Calculate Item Prices
WooCommerce also provides methods on the order object for calculating item prices with or without tax. These methods can be useful when building payment integrations or custom order calculations.
$order_id = 123;
$order = wc_get_order( $order_id );
if ( $order ) {
foreach ( $order->get_items() as $item ) {
// Include tax in the returned values.
$inc_tax = true;
// Do not round at the item level.
$round = false;
// Item price before discounts.
$item_cost_excl_disc = $order->get_item_subtotal(
$item,
$inc_tax,
$round
);
// Item price after discounts.
$item_cost_incl_disc = $order->get_item_total(
$item,
$inc_tax,
$round
);
// Tax amount for the item.
$item_tax_cost = $order->get_item_tax(
$item,
$inc_tax,
$round
);
// Line subtotal.
$item_line_subtotal = $order->get_line_subtotal(
$item,
$inc_tax,
$round
);
// Line total after discounts.
$item_line_total = $order->get_line_total(
$item,
$inc_tax,
$round
);
// Line tax.
$item_line_tax = $order->get_line_tax( $item );
echo 'Product: ' . esc_html( $item->get_name() );
echo ' | Item Price: ' . esc_html( $item_cost_incl_disc );
echo ' | Line Total: ' . esc_html( $item_line_total );
echo '<br>';
}
}
The $inc_tax argument controls whether tax is included in the calculated item prices. The $round argument controls rounding at the item level.
Choose the calculation method that matches your requirement. For straightforward order reporting, the order item methods from the previous examples are usually sufficient.
8. Get Order Item Metadata Using wc_get_order_item_meta()
If you are working with older WooCommerce code, you may come across the get_item_meta() method on order objects. For modern development, use the order item object’s get_meta() method or the wc_get_order_item_meta() function.
The function accepts three arguments:
wc_get_order_item_meta(
$item_id,
$key,
$single = true
);
- $item_id: The order item ID.
- $key: The metadata key to retrieve.
- $single: Whether to return a single value. The default is
true.
For example, you can retrieve the quantity and line total metadata like this:
$order_id = 123;
$order = wc_get_order( $order_id );
if ( $order ) {
foreach ( $order->get_items() as $item_id => $item ) {
$product_name = $item->get_name();
$item_quantity = wc_get_order_item_meta(
$item_id,
'_qty',
true
);
$item_total = wc_get_order_item_meta(
$item_id,
'_line_total',
true
);
echo 'Product: ' . esc_html( $product_name );
echo ' | Quantity: ' . esc_html( $item_quantity );
echo ' | Item Total: ' . esc_html( $item_total );
echo '<br>';
}
}
Although this function is available, using the order item methods such as get_quantity() and get_total() is generally clearer when working with standard product order data.
Important Notes
- WooCommerce version: The examples use the CRUD-based order and order item methods available in modern WooCommerce versions. The original article targeted WooCommerce 3.0 and later.
- Historical order prices: Use order item methods to retrieve the amounts saved when the order was placed. The product object’s current price may be different.
- Line total versus unit price:
get_total()returns the total for the entire order line after discounts, not the price of one unit. - Tax calculations: The standard order item subtotal and total methods exclude tax. Use the corresponding tax methods or order calculation methods when you need tax-inclusive amounts.
- Product availability: An order item may still exist even if its product has since been deleted. Always check whether
get_product()returns a valid product object. - Order item types:
$order->get_items()returns product line items by default. Shipping, fees, and coupons are retrieved using their respective order methods.
For more information, refer to the official WooCommerce WC_Order_Item_Product reference and the WC_Order class documentation.
If you are customizing order emails, you may also find our guide on displaying product images in WooCommerce order emails useful.
Conclusion
You can retrieve the price and quantity of ordered products using WooCommerce’s order and order item methods. Start by getting the order object with wc_get_order(), then loop through its product items using get_items().
Use get_quantity() to retrieve the ordered quantity, get_subtotal() to get the line subtotal before discounts, and get_total() to get the discounted line total. These methods provide the order values saved by WooCommerce and are generally the best choice when working with historical order data.