WooCommerce Hooks: Retrieve Order Data (Total, Items, and More) from the $order Object

WooCommerce Tutorial

How to Retrieve Order Data (Total, Items, and More) from the $order Object in WooCommerce – When working with WooCommerce, accessing detailed order information programmatically is essential for customizations, reporting, and integrations.

WooCommerce provides the $order object, which allows developers to fetch various details such as order totals, items, payment methods, customer details, shipping, and much more.

WooCommerce Hooks for Order Data: Retrieving Order Details from $order Object

Previously, we have covered the list of WooCommerce hooks for single product to retrieve that product data. In this article, we will cover every aspect of how to retrieve order-related data from the $order object, explaining how to get totals, items, shipping details, customer information, and more.

TL;DR

Hide
  • Developers can access detailed order information from the $order object in WooCommerce for customizations, reporting, and integrations.
  • Each WooCommerce order has a unique ID and key that can be retrieved using get_id() and get_order_key() methods.
  • WooCommerce stores detailed financial information for each order, including totals, taxes, shipping costs, and discounts, which can be fetched using various methods.
  • Refunded amounts can be retrieved using methods like get_total_refunded().
  • The $order object provides methods to retrieve and loop over each item in an order, including products, fees, and discounts, to gather details like product ID, name, quantity, and price.
  • Product meta information can be retrieved using get_meta_data().
  • Shipping details like shipping method, total cost, and tax can be accessed using the $order object.
  • WooCommerce orders have several important dates that can be accessed using methods like get_date_created() and get_date_paid().
  • Customer information, including billing and shipping addresses, user IDs, and IP addresses, can be retrieved using the $order object.
  • Payment information, such as the payment method used and transaction ID, can be retrieved using methods like get_payment_method() and get_transaction_id().
  • WooCommerce generates several URLs related to orders that can be retrieved programmatically.

This guide will help you understand the different methods available within the $order object and how to use them effectively in your WooCommerce store customization.

The same methods I use when I built OneClick Chat to Order and all its add-ons such as OneClick WCFM Connector, and OneClick Dokan Connector.

Here’s how to get all the order information in WooCommerce using WooCommerce hooks for order data:

// 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();
   $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();
}
 
// 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();

Retrieving Order ID and Key

Every WooCommerce order has a unique ID and key. These values are essential for referencing specific orders when performing tasks such as building integrations, running analytics, or handling custom operations.

You can retrieve the order’s ID and key using the following methods:

$order->get_id(); // Get the unique order ID
$order->get_order_key(); // Get the order key
  • get_id() returns the unique order ID.
  • get_order_key() returns the order’s unique key, which is useful for order validation and security checks.

Retrieving Order Totals and Financial Information

WooCommerce stores detailed financial information for each order, including the total amount, taxes, shipping costs, and any discounts applied.

This information provides a complete picture of an order’s financial data.

$order->get_formatted_order_total(); // Get formatted total amount of the order
$order->get_cart_tax(); // Get the cart tax amount
$order->get_currency(); // Get the currency code (e.g., USD)
$order->get_discount_tax(); // Get the total discount tax
$order->get_discount_to_display(); // Get the formatted discount display
$order->get_discount_total(); // Get the total discount applied to the order
$order->get_total_fees(); // Get the total fees applied to the order
$order->get_formatted_line_subtotal(); // Get formatted line subtotal
$order->get_shipping_tax(); // Get the total tax applied to shipping
$order->get_shipping_total(); // Get the total shipping cost
$order->get_subtotal(); // Get the subtotal before tax and shipping
$order->get_subtotal_to_display(); // Get formatted subtotal for display
$order->get_tax_location(); // Get the tax location for the order
$order->get_tax_totals(); // Get the total tax breakdown
$order->get_taxes(); // Get all the tax data for the order
$order->get_total(); // Get the total order amount
$order->get_total_discount(); // Get the total amount of discounts applied
$order->get_total_tax(); // Get the total tax for the order
$order->get_total_refunded(); // Get the total amount refunded
$order->get_total_tax_refunded(); // Get the total tax refunded
$order->get_total_shipping_refunded(); // Get the refunded shipping cost
$order->get_item_count_refunded(); // Get the count of refunded items
$order->get_total_qty_refunded(); // Get the total quantity of refunded items
$order->get_qty_refunded_for_item(); // Get the refunded quantity for a specific item
$order->get_total_refunded_for_item(); // Get the total refunded amount for a specific item
$order->get_tax_refunded_for_item(); // Get the refunded tax amount for a specific item
$order->get_total_tax_refunded_by_rate_id(); // Get refunded tax amount by tax rate ID
$order->get_remaining_refund_amount(); // Get the remaining refund amount for the order

These methods allow you to fetch details like the subtotal, total amount, shipping costs, tax breakdowns, and any applied discounts or fees.

For example:

  • get_total() will return the final total for the order.
  • get_total_tax() provides the total tax amount, while get_shipping_total() gives you the cost of shipping.

If the order includes a refund, you can use methods like get_total_refunded() to get the refunded amount.


Retrieving and Looping Over Order Items

Every order contains one or more items, which can include products, fees, or discounts. The $order object provides a method to retrieve and loop over each item to gather details like the product ID, name, quantity, and price.

foreach ( $order->get_items() as $item_id => $item ) {
   $product_id = $item->get_product_id(); // Get the product ID
   $variation_id = $item->get_variation_id(); // Get the variation ID (if any)
   $product = $item->get_product(); // Get the product object
   $product_name = $item->get_name(); // Get the product name
   $quantity = $item->get_quantity(); // Get the quantity of the item
   $subtotal = $item->get_subtotal(); // Get the item subtotal (before tax)
   $total = $item->get_total(); // Get the item total (after tax)
   $tax = $item->get_subtotal_tax(); // Get the tax for the item
   $tax_class = $item->get_tax_class(); // Get the item's tax class
   $tax_status = $item->get_tax_status(); // Get the tax status (taxable or not)
   $allmeta = $item->get_meta_data(); // Get all meta data for the item
   $somemeta = $item->get_meta( '_custom_key', true ); // Get specific meta data
   $item_type = $item->get_type(); // Get the type of item (line_item, fee, etc.)
}

In this loop:

  • get_product_id() returns the ID of the product in the order.
  • get_quantity() gives the quantity of each item in the order.
  • get_total() provides the total amount for each item after tax.

You can also retrieve product meta information such as custom fields or other attributes using get_meta_data().


Retrieving Shipping Information

Shipping details like shipping method, total cost, and tax can be easily accessed using the $order object.

This is especially important when generating invoices or providing shipping details to customers.

$order->get_shipping_method(); // Get the shipping method used
$order->get_shipping_methods(); // Get all shipping methods applied to the order
$order->get_shipping_to_display(); // Get formatted shipping information for display
  • get_shipping_method() returns the shipping method used (e.g., “Free Shipping”).
  • get_shipping_to_display() returns a formatted string of shipping costs and methods.

Retrieving Order Dates

Orders in WooCommerce have several important dates, such as when the order was created, paid, completed, or modified. These methods provide access to all of these dates.

$order->get_date_created(); // Get the date the order was created
$order->get_date_modified(); // Get the date the order was last modified
$order->get_date_completed(); // Get the date the order was marked complete
$order->get_date_paid(); // Get the date the order was paid

These methods return the respective dates in a DateTime format, which you can use for tracking and analytics.


Retrieving Customer and Billing/Shipping Address Information

WooCommerce stores detailed customer information, including billing and shipping addresses, user IDs, and even IP addresses. You can easily retrieve this data using the $order object.

$order->get_customer_id(); // Get the WooCommerce customer ID
$order->get_user_id(); // Get the WordPress user ID (if registered user)
$order->get_user(); // Get the WP_User object
$order->get_customer_ip_address(); // Get the customer's IP address
$order->get_customer_user_agent(); // Get the user's browser user agent
$order->get_created_via(); // Get how the order was created (admin, checkout, etc.)
$order->get_customer_note(); // Get customer notes attached to the order
$order->get_address_prop(); // Get any address property (billing or shipping)
$order->get_billing_first_name(); // Get billing first name
$order->get_billing_last_name(); // Get billing last name
$order->get_billing_company(); // Get billing company name
$order->get_billing_address_1(); // Get billing address line 1
$order->get_billing_address_2(); // Get billing address line 2
$order->get_billing_city(); // Get billing city
$order->get_billing_state(); // Get billing state
$order->get_billing_postcode(); // Get billing postcode
$order->get_billing_country(); // Get billing country
$order->get_billing_email(); // Get billing email address
$order->get_billing_phone(); // Get billing phone number
$order->get_shipping_first_name(); // Get shipping first name
$order->get_shipping_last_name(); // Get shipping last name
$order->get_shipping_company(); // Get shipping company name
$order->get_shipping_address_1(); // Get shipping address line 1
$order->get_shipping_address_2(); // Get shipping address line 2
$order->get_shipping_city(); // Get shipping city
$order->get_shipping_state(); // Get shipping state
$order->get_shipping_postcode(); // Get shipping postcode
$order->get_shipping_country(); // Get shipping country

For each order, you can access detailed billing and shipping addresses along with customer-related information. This data can be useful for custom notifications, generating invoices, or integrating with third-party platforms.


Retrieving Order Payment Details

WooCommerce orders include payment information, such as the payment method used and transaction ID. You can retrieve this data using the following methods:

$order->get_payment_method(); // Get the payment method used (e.g., PayPal, Stripe)
$order->get_payment_method_title(); // Get the payment method title
$order->get_transaction_id(); // Get the transaction ID from the payment gateway

These methods are particularly useful for generating reports or handling payment processing in custom systems.


Retrieving Order URLs

WooCommerce generates several URLs related to orders, such as the checkout, thank you, and view order pages. You can retrieve these URLs programmatically.

$order->get_checkout_payment_url(); // Get the checkout payment URL
$order->get_checkout_order_received_url(); // Get the thank you page URL
$order->get_cancel_order_url(); // Get the cancel order URL
$order->get_cancel_order_url_raw(); // Get the raw cancel order URL
$order->get_cancel_endpoint(); // Get the cancel endpoint
$order->get_view_order_url(); // Get the view order URL for customers
$order->get_edit_order_url(); // Get the edit order URL (admin)

These URLs are useful for sending customers back to specific pages or for integrating custom order management flows.


Wrapping Up

By understanding how to retrieve data from the $order object in WooCommerce, you can build custom solutions to enhance your store’s functionality.

Whether you’re creating detailed reports, integrating with third-party services, or building custom workflows, WooCommerce’s extensive set of methods for the $order object provides everything you need to access order details.

From order totals and items to shipping and customer information, this guide covers all the essential methods for extracting order data programmatically.

Related Posts