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

How Can I Retrieve WooCommerce Order Details Using the Order ID?

Linda Hamilton
Release: 2024-12-23 22:07:18
Original
562 people have browsed it

How Can I Retrieve WooCommerce Order Details Using the Order ID?

How can I get WooCommerce order details from Order ID?

WooCommerce allows us to retrieve extensive order details using the wc_get_order() function. This function accepts an order ID as an argument and returns a WC_Order object.

Using the WC_Order object, you can access various order-related details through its methods. Here's an example:

$order = wc_get_order( $order_id );

$order_id = $order->get_id(); // Get the order ID
$order_status = $order->get_status(); // Get the order status
$order_total = $order->get_total(); // Get the order total
Copy after login

Version 3.0 Updates:

  • Properties can no longer be accessed directly, as they were in earlier versions.
  • New getter and setter methods are required.
  • Data can be accessed using the get_data() method.

Accessing Order Items:

You can iterate through order items and access detailed information for each one:

foreach ($order->get_items() as $item_key => $item) {
    $product_id = $item->get_product_id();
    $quantity = $item->get_quantity();
    $line_subtotal = $item->get_subtotal();
}
Copy after login

Retrieving Billing and Shipping Information:

The WC_Order object also provides access to billing and shipping information:

$billing_first_name = $order->get_billing_first_name();
$shipping_country = $order->get_shipping_country();
Copy after login

Order Data Properties:

Using the get_data() method, you can access order data properties as an array of values:

$order_data = $order->get_data();

$order_id = $order_data['id'];
$order_status = $order_data['status'];
Copy after login

By utilizing these methods and accessing the appropriate data structures, you can effectively retrieve various order details in WooCommerce.

The above is the detailed content of How Can I 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