Home >Backend Development >PHP Tutorial >How to Retrieve Detailed WooCommerce Order Information Using Order ID?

How to Retrieve Detailed WooCommerce Order Information Using Order ID?

Linda Hamilton
Linda HamiltonOriginal
2024-12-19 09:54:09863browse

How to Retrieve Detailed WooCommerce Order Information Using Order ID?

How to Get WooCommerce Order Details

WooCommerce provides various methods to retrieve detailed information about an order using its order ID. To access order details, you can use the WC_Order and WC_Abstract_Order classes.

WooCommerce Orders in Version 3.0

In WooCommerce versions 3.0 and above, the following changes were introduced:

  • Order properties can no longer be accessed directly as before.
  • You must use specific getter and setter methods to access order data.
  • New classes were introduced for order items, including WC_Order_Item, WC_Order_Item_Product, and others.
  • The WC_Data Abstract class provides methods for accessing order and order item data using get_data(), get_meta_data(), and get_meta().

Retrieving Order Details

To retrieve order details, follow these steps:

  1. Get an instance of the WC_Order object:

    $order = wc_get_order( $order_id );
  2. Use getter methods to access specific order properties:

    $order_id  = $order->get_id(); // Order ID
    $status      = $order->get_status(); // Order status
    $currency      = $order->get_currency(); // Currency used
    $payment_method = $order->get_payment_method(); // Payment method ID
    $date_created  = $order->get_date_created(); // Date created (WC_DateTime object)

Additional Details

  • You can use the has_status() conditional method to check for specific order statuses.
  • To retrieve order data as an array of values, use the get_data() method.
  • Billing and shipping information can be accessed using dedicated getter methods.

Order Items

To get order items and their details:

  1. Iterate through the $order->get_items() collection:

    foreach ($order->get_items() as $item_key => $item) {
     $product_id = $item->get_product_id(); // Product ID
     $quantity = $item->get_quantity(); // Quantity
     $total = $item->get_total(); // Total price
    }
  2. Use WC_Order_Item_Product methods to access product-specific information:

    $product = $item->get_product(); // WC_Product object
    $product_type = $product->get_type(); // Product type

By utilizing these methods, you can easily access detailed information about WooCommerce orders and order items.

The above is the detailed content of How to Retrieve Detailed WooCommerce Order Information Using Order ID?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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