A Comprehensive Guide to Retrieving Order Item Price and Quantity in Woocommerce

Dec 08, 2025 · Programming · 10 views · 7.8

Keywords: Woocommerce | order item price | order item quantity | PHP | WordPress

Abstract: This article provides an in-depth analysis of methods to retrieve order item price and quantity in Woocommerce, covering versions from 3+ to earlier ones. It discusses the object-oriented API changes, offers rewritten code examples, and emphasizes best practices. Topics include using WC_Order_Item_Product, WC_Abstract_Order, and WC_Data classes, with a focus on modularity and version compatibility to support technical blog needs.

Introduction

In Woocommerce development, accessing order item data such as price and quantity is a common requirement. However, the methods to retrieve this information vary across different versions of Woocommerce. Based on the best answer, this guide offers a systematic approach to solve issues in Woocommerce 2.6.8 and later versions.

Version Compatibility

Woocommerce 3+ introduced new object-oriented APIs, making it easier to handle order items. For earlier versions, such as Woocommerce 2.4 to 2.6.x, different methods are required. Here is a core overview:

Core Methods in Woocommerce 3+

Use wc_get_order() to obtain the order object, then iterate through items returned by get_items(). Here, the WC_Order_Item_Product class provides various methods to access order item data. Below is a rewritten code example based on a deep understanding of the item objects:

$order = wc_get_order($order_id);
foreach ($order->get_items() as $item_id => $item) {
    $product = $item->get_product();
    $item_quantity = $item->get_quantity();
    $item_total = $item->get_total();
    echo "Product name: " . $item->get_name() . " | Quantity: " . $item_quantity . " | Total: " . number_format($item_total, 2);
}

This example demonstrates how to use methods like get_quantity() and get_total(), which are detailed in the official documentation.

Legacy Methods for Earlier Versions

For Woocommerce 2.4 to 2.6.x, the get_item_meta() method is still available but deprecated in newer versions. Here is a rewritten example:

$order = wc_get_order($order_id);
$order_items = $order->get_items();
foreach ($order_items as $item_id => $item) {
    $product_name = $item['name'];
    $item_quantity = $order->get_item_meta($item_id, '_qty', true);
    $item_total = $order->get_item_meta($item_id, '_line_total', true);
    echo "Product name: $product_name | Quantity: $item_quantity | Total: $item_total";
}

In the code, note keys like _qty and _line_total, which are common meta keys stored in order item data.

Using WC_Abstract_Order and WC_Data Methods

Beyond basic methods, the WC_Abstract_Order class offers additional options, such as including taxes and rounding. Example:

foreach ($order->get_items() as $item_id => $item) {
    $inc_tax = true;
    $round = false;
    $item_total = $order->get_item_total($item, $inc_tax, $round);
    echo "Adjusted total: " . $item_total;
}

Additionally, the WC_Data class methods like get_data() and get_meta_data() can be used to retrieve meta data arrays.

Best Practices and Conclusion

It is recommended to always refer to the official documentation to ensure code compatibility. Use the wc_get_order_item_meta() function instead of the deprecated get_item_meta() method, especially in Woocommerce 3+. Moreover, debug code and test across versions to avoid unexpected errors. By reorganizing the logical structure, this article provides a comprehensive perspective to help developers efficiently handle order data in the Woocommerce environment.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.