Home > Backend Development > PHP Tutorial > How to Retrieve WooCommerce Order Details Using the Order ID?

How to Retrieve WooCommerce Order Details Using the Order ID?

Linda Hamilton
Release: 2024-12-22 20:21:18
Original
543 people have browsed it

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 );
Copy after login

Updated Approach (Version 3.0 )

$order = wc_get_order( $order_id );
Copy after login

Once you have an instance of the WC_Order object, you can access order details using getter methods. For example:

  • Order ID: $order->get_id();
  • Order Status: $order->get_status();
  • Currency: $order->get_currency();
  • Payment Method: $order->get_payment_method();
  • Date Created: $order->get_date_created();
  • Customer ID: $order->get_user_id();
  • Total Tax: $order->get_total_tax();

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();
}
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template