WooCommerce, the popular e-commerce plugin for WordPress, offers a robust platform for creating and managing online stores. One essential task for store owners and developers is retrieving order details to manage and process sales efficiently. This article delves into the methods and best practices for accessing WooCommerce order details, providing a step-by-step guide to help you get started.
Understanding WooCommerce Order Details
In WooCommerce, an order is a record of a purchase made by a customer. Each order contains various details, including customer information, products purchased, shipping and billing addresses, payment methods, and order status. Accessing these details is crucial for fulfilling orders, generating reports, and integrating with other systems.
Methods to Retrieve WooCommerce Order Details
WooCommerce provides several ways to retrieve order details, ranging from using built-in functions to leveraging the WooCommerce REST API. Below, we explore these methods in detail.
1. Using Built-in WooCommerce Functions
WooCommerce offers a range of PHP functions to retrieve order details. These functions are particularly useful for theme and plugin developers who need to access order information within WordPress.
a. Accessing Order Data with wc_get_order()
The wc_get_order()
function retrieves an order object by its ID. This object provides access to all order details through various methods.
php$order_id = 123; // Replace with your order ID
$order = wc_get_order($order_id);
if ($order) {
echo 'Order ID: ' . $order->get_id();
echo 'Order Total: ' . $order->get_total();
echo 'Customer Email: ' . $order->get_billing_email();
// Add more details as needed
}
b. Retrieving Order Items
To get details about the products in an order, you can loop through the order items.
php$order_items = $order->get_items();
foreach ($order_items as $item_id => $item) {
$product_name = $item->get_name();
$product_quantity = $item->get_quantity();
$product_total = $item->get_total();
echo 'Product: ' . $product_name;
echo 'Quantity: ' . $product_quantity;
echo 'Total: ' . $product_total;
}
c. Accessing Billing and Shipping Information
You can also retrieve billing and shipping details using the order object's methods.
php$billing_address = $order->get_formatted_billing_address();
$shipping_address = $order->get_formatted_shipping_address();
echo 'Billing Address: ' . $billing_address;
echo 'Shipping Address: ' . $shipping_address;
2. Using WooCommerce REST API
For more advanced integrations, especially those involving external systems, the WooCommerce REST API is a powerful tool. It allows you to access order details over HTTP, making it ideal for use with different programming languages and platforms.
a. Authentication
To use the REST API, you need to authenticate your requests. WooCommerce supports both OAuth 1.0a and basic authentication using consumer keys and secrets.
bashcurl https://example.com/wp-json/wc/v3/orders/123 \ -u consumer_key:consumer_secret
b. Retrieving an Order
Using the REST API, you can retrieve order details by sending a GET request to the /orders
endpoint.
bashcurl https://example.com/wp-json/wc/v3/orders/123 \ -u consumer_key:consumer_secret
The response will be a JSON object containing all order details.
json{
"id": 123,
"parent_id": 0,
"status": "completed",
"currency": "USD",
"total": "29.99",
"billing": {
"first_name": "John",
"last_name": "Doe",
"address_1": "123 Main St",
"city": "Anytown",
"postcode": "12345",
"country": "US",
"email": "
This e-mail address is being protected from spambots. You need JavaScript enabled to view it
",
"phone": "(555) 555-5555"
},
"shipping": {
"first_name": "John",
"last_name": "Doe",
"address_1": "123 Main St",
"city": "Anytown",
"postcode": "12345",
"country": "US"
},
"line_items": [
{
"id": 1,
"name": "T-Shirt",
"product_id": 10,
"quantity": 1,
"total": "19.99"
}
]
}
c. Filtering Orders
You can also filter orders by various parameters, such as status, customer, date, and more.
bashcurl https://example.com/wp-json/wc/v3/orders?status=completed \ -u consumer_key:consumer_secret
This will return all completed orders.
Conclusion
Retrieving WooCommerce order details is a fundamental task for managing your online store effectively. Whether you prefer using built-in WooCommerce functions or the more versatile WooCommerce REST API, understanding how to access and manipulate order data is essential for developers and store owners. By following the methods outlined in this guide, you can seamlessly integrate order details into your workflows and systems, ensuring a smooth and efficient e-commerce operation.
Here are additional code snippets to help you further explore and utilize WooCommerce order details.
1. Detailed Order Information Retrieval with PHP
a. Getting All Order Data
This snippet demonstrates how to fetch and display comprehensive order details.
php$order_id = 123; // Replace with your order ID
$order = wc_get_order($order_id);
if ($order) {
// Order ID and status
echo 'Order ID: ' . $order->get_id();
echo 'Order Status: ' . $order->get_status();
// Order totals
echo 'Order Total: ' . $order->get_total();
echo 'Order Subtotal: ' . $order->get_subtotal();
echo 'Order Tax: ' . $order->get_total_tax();
echo 'Order Shipping: ' . $order->get_shipping_total();
// Payment details
echo 'Payment Method: ' . $order->get_payment_method_title();
// Customer details
echo 'Customer ID: ' . $order->get_customer_id();
echo 'Customer Email: ' . $order->get_billing_email();
echo 'Customer Phone: ' . $order->get_billing_phone();
// Billing address
echo 'Billing Address: ' . $order->get_formatted_billing_address();
// Shipping address
echo 'Shipping Address: ' . $order->get_formatted_shipping_address();
// Order dates
echo 'Order Date: ' . $order->get_date_created()->date('Y-m-d H:i:s');
echo 'Paid Date: ' . ($order->get_date_paid() ? $order->get_date_paid()->date('Y-m-d H:i:s') : 'Not Paid');
echo 'Completed Date: ' . ($order->get_date_completed() ? $order->get_date_completed()->date('Y-m-d H:i:s') : 'Not Completed');
}
b. Iterating Over Order Items with Meta Data
This snippet shows how to loop through order items and display product meta data.
php$order_items = $order->get_items();
foreach ($order_items as $item_id => $item) {
$product_name = $item->get_name();
$product_quantity = $item->get_quantity();
$product_total = $item->get_total();
echo 'Product: ' . $product_name;
echo 'Quantity: ' . $product_quantity;
echo 'Total: ' . $product_total;
// Accessing product meta data
$item_meta_data = $item->get_meta_data();
foreach ($item_meta_data as $meta) {
echo $meta->key . ': ' . $meta->value;
}
}
2. WooCommerce REST API Usage
a. Fetching Orders Using the REST API
This example demonstrates how to retrieve order details using the WooCommerce REST API with cURL in PHP.
php$consumer_key = 'ck_your_consumer_key';
$consumer_secret = 'cs_your_consumer_secret';
$order_id = 123;
$url = "https://example.com/wp-json/wc/v3/orders/{$order_id}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$consumer_key:$consumer_secret");
$response = curl_exec($ch);
curl_close($ch);
$order = json_decode($response, true);
if ($order) {
echo 'Order ID: ' . $order['id'];
echo 'Order Total: ' . $order['total'];
echo 'Customer Email: ' . $order['billing']['email'];
foreach ($order['line_items'] as $item) {
echo 'Product: ' . $item['name'];
echo 'Quantity: ' . $item['quantity'];
echo 'Total: ' . $item['total'];
}
}
b. Filtering Orders by Date Range
You can filter orders based on a specific date range using the REST API.
php$consumer_key = 'ck_your_consumer_key';
$consumer_secret = 'cs_your_consumer_secret';
$start_date = '2023-01-01T00:00:00';
$end_date = '2023-01-31T23:59:59';
$url = "https://example.com/wp-json/wc/v3/orders?after={$start_date}&before={$end_date}";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$consumer_key:$consumer_secret");
$response = curl_exec($ch);
curl_close($ch);
$orders = json_decode($response, true);
foreach ($orders as $order) {
echo 'Order ID: ' . $order['id'];
echo 'Order Total: ' . $order['total'];
echo 'Customer Email: ' . $order['billing']['email'];
}
3. Adding Custom Order Meta Data
a. Adding Custom Meta Data to an Order
This snippet shows how to add custom meta data to an order.
php$order_id = 123;
$order = wc_get_order($order_id);
if ($order) {
$order->update_meta_data('custom_meta_key', 'custom_meta_value');
$order->save();
echo 'Custom meta data added successfully!';
}
b. Retrieving Custom Meta Data from an Order
This example demonstrates how to retrieve custom meta data from an order.
php$order_id = 123;
$order = wc_get_order($order_id);
if ($order) {
$custom_meta_value = $order->get_meta('custom_meta_key');
echo 'Custom Meta Value: ' . $custom_meta_value;
}
These additional code snippets should help you better understand how to work with WooCommerce order details, whether you're developing custom functionality within WordPress or integrating WooCommerce with external systems using the REST API.