WooCommerce Get Order Data From the $order Object
WooCommerce Get Order Data is a common requirement when working with WooCommerce, especially if you’re building custom plugins, and integrations, or looking to enhance your store’s functionality. The $order
object in WooCommerce serves as the primary tool for accessing all the details of an order programmatically.
In this guide, we’ll explore how to retrieve essential order data—like the total amount, items, customer details, and more—using the $order
object.
As a WooCommerce developer, I often find myself performing the same repetitive coding tasks, which can be a real time sink. One common scenario is figuring out how to retrieve specific pieces of information from the $order
object. For example:
- How do I get the order total?
- How can I access the order items?
- What about the order dates, customer ID, billing details, payment method, or the current order status?
These are frequent questions, and this guide is designed to help you quickly find the answers!
If you’ve worked with WooCommerce, you’ve probably encountered similar objects like $product
for product data or $cart
for cart details. Accessing the $order
object works similarly but comes with its own nuances.
Sometimes, you won’t have direct access to the $order
object but will have the order ID instead. In such cases, you can retrieve the $order
object using the wc_get_order()
function, a handy WooCommerce utility.
Additionally, if you’re customizing email templates, you might need $order
information to add personalized content or trigger custom actions. This guide will empower you to make the most out of the $order
object for such tasks.
Dive in and save time with these essential WooCommerce tips!
How to Get the $order
Object or WooCommerce Get Order Data
Before accessing any order data, you need to get the $order
object. There are two common scenarios for this:
1.When you have the order ID
Use WooCommerce’s wc_get_order()
function:
$order_id = 123; // Replace with your order ID
$order = wc_get_order( $order_id );
// Get Order ID and Key
$order->get_id();
$order->get_order_key();
// Get Order Totals
$order->get_formatted_order_total();
$order->get_cart_tax();
$order->get_currency();
$order->get_discount_tax();
$order->get_discount_to_display();
$order->get_discount_total();
$order->get_total_fees();
$order->get_formatted_line_subtotal();
$order->get_shipping_tax();
$order->get_shipping_total();
$order->get_subtotal();
$order->get_subtotal_to_display();
$order->get_tax_location();
$order->get_tax_totals();
$order->get_taxes();
$order->get_total();
$order->get_total_discount();
$order->get_total_tax();
$order->get_total_refunded();
$order->get_total_tax_refunded();
$order->get_total_shipping_refunded();
$order->get_item_count_refunded();
$order->get_total_qty_refunded();
$order->get_qty_refunded_for_item();
$order->get_total_refunded_for_item();
$order->get_tax_refunded_for_item();
$order->get_total_tax_refunded_by_rate_id();
$order->get_remaining_refund_amount();
// Get and Loop Over Order Items
foreach ( $order->get_items() as $item_id => $item ) {
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
$product = $item->get_product(); // see link above to get $product info
$product_name = $item->get_name();
$quantity = $item->get_quantity();
$subtotal = $item->get_subtotal();
$total = $item->get_total();
$tax = $item->get_subtotal_tax();
$tax_class = $item->get_tax_class();
$tax_status = $item->get_tax_status();
$allmeta = $item->get_meta_data();
$somemeta = $item->get_meta( '_whatever', true );
$item_type = $item->get_type(); // e.g. "line_item", "fee"
}
// Other Secondary Items Stuff
$order->get_items_key();
$order->get_items_tax_classes();
$order->get_item_count();
$order->get_item_total();
$order->get_downloadable_items();
$order->get_coupon_codes();
// Get Order Lines
$order->get_line_subtotal();
$order->get_line_tax();
$order->get_line_total();
// Get Order Shipping
$order->get_shipping_method();
$order->get_shipping_methods();
$order->get_shipping_to_display();
// Get Order Dates
$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();
// Get Order User, Billing & Shipping Addresses
$order->get_customer_id();
$order->get_user_id();
$order->get_user();
$order->get_customer_ip_address();
$order->get_customer_user_agent();
$order->get_created_via();
$order->get_customer_note();
$order->get_address_prop();
$order->get_billing_first_name();
$order->get_billing_last_name();
$order->get_billing_company();
$order->get_billing_address_1();
$order->get_billing_address_2();
$order->get_billing_city();
$order->get_billing_state();
$order->get_billing_postcode();
$order->get_billing_country();
$order->get_billing_email();
$order->get_billing_phone();
$order->get_shipping_first_name();
$order->get_shipping_last_name();
$order->get_shipping_company();
$order->get_shipping_address_1();
$order->get_shipping_address_2();
$order->get_shipping_city();
$order->get_shipping_state();
$order->get_shipping_postcode();
$order->get_shipping_country();
$order->get_address();
$order->get_shipping_address_map_url();
$order->get_formatted_billing_full_name();
$order->get_formatted_shipping_full_name();
$order->get_formatted_billing_address();
$order->get_formatted_shipping_address();
// Get Order Payment Details
$order->get_payment_method();
$order->get_payment_method_title();
$order->get_transaction_id();
// Get Order URLs
$order->get_checkout_payment_url();
$order->get_checkout_order_received_url();
$order->get_cancel_order_url();
$order->get_cancel_order_url_raw();
$order->get_cancel_endpoint();
$order->get_view_order_url();
$order->get_edit_order_url();
// Get Order Status
$order->get_status();
// Get Thank You Page URL
$order->get_checkout_order_received_url();
2.Inside WooCommerce Hooks or Filters
If you’re using a WooCommerce hook like woocommerce_thankyou
, the $order_id
is passed as a parameter, and you can retrieve the $order
object like this:
add_action( 'woocommerce_thankyou', 'my_custom_function', 10, 1 );
function my_custom_function( $order_id ) {
$order = wc_get_order( $order_id );
// Now you can access $order details
}
1. General Order Details
Data | Method | Example Output |
---|---|---|
Order ID | $order->get_id() | 123 |
Order Number | $order->get_order_number() | #1005 |
Order Status | $order->get_status() | completed , pending , etc. |
Order Date | $order->get_date_created() | 2024-01-01 12:00:00 |
Order Modified Date | $order->get_date_modified() | 2024-01-02 14:00:00 |
Currency | $order->get_currency() | USD , EUR , etc. |
Order Total | $order->get_total() | 150.00 |
Order Subtotal | $order->get_subtotal() | 120.00 |
Discount Total | $order->get_discount_total() | 10.00 |
Total Tax | $order->get_total_tax() | 20.00 |
Transaction ID | $order->get_transaction_id() | txn_123456789 |
Order Key | $order->get_order_key() | wc_order_abcdef123456 |
2. Customer Information
Data | Method | Example Output |
---|---|---|
Customer ID | $order->get_customer_id() | 1 (for registered users) |
Billing First Name | $order->get_billing_first_name() | John |
Billing Last Name | $order->get_billing_last_name() | Doe |
Billing Full Name | $order->get_formatted_billing_full_name() | John Doe |
Billing Email | $order->get_billing_email() | john.doe@example.com |
Billing Phone | $order->get_billing_phone() | +123456789 |
Billing Address | $order->get_billing_address() | 123 Street, City |
Shipping Full Name | $order->get_formatted_shipping_full_name() | John Doe |
Shipping Address | $order->get_formatted_shipping_address() | 123 Street, City |
3. Order Items
Data | Method | Example Output |
---|---|---|
Items in the Order | $order->get_items() | Array of items |
Product Name | $item->get_name() | T-Shirt |
Product ID | $item->get_product_id() | 456 |
Variation ID | $item->get_variation_id() | 457 (if a variation product) |
Quantity | $item->get_quantity() | 2 |
Line Subtotal | $item->get_subtotal() | 40.00 |
Line Total | $item->get_total() | 80.00 |
Tax for Line | $item->get_taxes() | Tax array |
4. Payment Details
Data | Method | Example Output |
---|---|---|
Payment Method | $order->get_payment_method() | paypal , stripe , etc. |
Payment Method Title | $order->get_payment_method_title() | PayPal |
Is Payment Complete? | $order->is_paid() | true or false |
5. Shipping Details
Shipping Total : $order->get_shipping_total();
Shipping Tax : $order->get_shipping_tax();
Shipping Method : $order->get_shipping_method();
6. Coupons Used in the Order
To get the coupons applied to the order:
$coupons = $order->get_coupon_codes();
foreach ( $coupons as $coupon ) {
echo 'Coupon Code: ' . $coupon;
}
7. Metadata (Custom Fields)
If you’ve added custom metadata to the order:
$meta_data = $order->get_meta( '_custom_meta_key' );
echo 'Custom Meta: ' . $meta_data;
Iterating Over Items
To iterate over the order items, including product details:
$items = $order->get_items();
foreach ( $items as $item_id => $item ) {
$product = $item->get_product(); // WC_Product object
echo 'Product Name: ' . $item->get_name() . '<br>';
echo 'Quantity: ' . $item->get_quantity() . '<br>';
echo 'Price: ' . wc_price( $item->get_total() ) . '<br>';
}
Debugging: View All Order Data
To view everything available in the $order
object for debugging:
echo '<pre>';
print_r( $order );
echo '</pre>';
Conclusion
The $order
object in WooCommerce provides an extensive API to access all the order-related data you need. By leveraging the methods listed above, you can extract and use this data to customize order handling, reporting, and integrations.