Home >Backend Development >PHP Tutorial >How to Retrieve WooCommerce Order Details Using the Order ID?
How to Get WooCommerce Order Details from the Order ID
To retrieve WooCommerce order details, you can use either the legacy process or the updated method introduced in version 3.0.
Legacy Approach
$order = new WC_Order( $order_id );
Updated Approach (Version 3.0 )
$order = wc_get_order( $order_id );
Once you have an instance of the WC_Order object, you can access order details using getter methods. For example:
Retrieving Order Item Details
To retrieve order item details, you need to iterate through the order items. In version 3.0 , this can be done using the following snippet:
foreach ($order->get_items() as $item) { $product_name = $item->get_name(); $quantity = $item->get_quantity(); $line_total = $item->get_total(); }
Additional Notes
In version 3.0 , accessing order properties directly (e.g., $order->order_id) is no longer possible. You must use the appropriate getter methods instead. Additionally, you can access order data as an associative array using the get_data() method.
The above is the detailed content of How to Retrieve WooCommerce Order Details Using the Order ID?. For more information, please follow other related articles on the PHP Chinese website!