Website Building » WooCommerce » How Do I Get WooCommerce Order Details in PHP?

How Do I Get WooCommerce Order Details in PHP?

Last updated on January 21, 2023 @ 2:21 pm

If you’re running a WooCommerce store, you might need to get some order details in PHP for various purposes. For example, you might need to get the order number, customer name, billing address, shipping address, etc.

Fortunately, WooCommerce makes it easy to get this information. In this article, we’ll show you how to get WooCommerce order details in PHP.

Getting the Order Number

If you need to get the order number, you can use the get_order_number() function. For example:

PRO TIP: When using the WooCommerce plugin for WordPress, it is possible to get order details in PHP using the WC_Order class. However, this class is not intended for public use and its methods and properties are subject to change without notice. Therefore, it is strongly recommended that you use the WooCommerce REST API to get order information instead.
$order_number = $order->get_order_number();

Getting the Customer Name

To get the customer name, you can use the get_billing_first_name(), get_billing_last_name(), and get_billing_company() functions. For example:

$first_name = $order->get_billing_first_name();
$last_name = $order->get_billing_last_name();
$company = $order->get_billing_company();

Getting the Billing Address

To get the billing address, you can use the

get_billing_address_1(), get_billing_address_2(), get_billing_city(), get_billing_state(), get_billing_postcode(), and get_billing_country() functions. For example:

$address_1 = $order->get_billing_address_1();
$address_2 = $order->get_billing_address_2();
$city = $order->get_billing_city();
$state = $order->get_billing_state();
$postcode = $order->get_billing_postcode();
$country = $order->get_billing_country();

Getting the Shipping Address

To get the shipping address, you can use the get_shipping_address_1(), get_shipping_address_2(), get_shipping_city(), get_shipping_state(), get_shipping_postcode(), and get_shipping_country() functions. For example:

$shipping_address_1 = $order->get_shipping_address_1();
$shipping_address_2 = $order->get_shipping_address_2();
$shipping_city = $order->get_shipping_city();
$shipping_state = $order->get_shipping_state();
$shipping_postcode = $order->get_shipping_postcode();
$shipping_country = $order->get_shipping_country();

Retrieving Order Items

You can also get the items that were ordered by calling the get_items() function. This will return an array of WC_Order_Item_Product objects, which you can use to get details about each item such as the name, quantity, and price.

foreach( $order->get_items() as $item_id => $item_product ){
$product_name = $item_product->get_name();
$product_quantity = $item_product->get_quantity();
$product_price = $item_product->get_total();
}

Note: $order is an instance of WC_Order, and you will need to pass an order_id or an order object to get order details.

These are just a few examples of how you can use the WooCommerce functions to get order details in PHP. There are many other functions available for getting additional information about an order, so be sure to check the WooCommerce documentation for more information.

Morgan Bash

Morgan Bash

Technology enthusiast and Co-Founder of Women Coders SF.