Get Order Info From The WooCommerce Order Object
Once you have the WooCommerce $order object, you can easily access any data or information contained within any given WooCommerce order.
For this tutorial, you will be adding your PHP to the functions.php file.
If you are not sure how to access functions.php you should read our article on how to edit functions.php.
How To Get The WooCommerce Order Object
If you already have the $order_id
First thing’s first, let’s make sure you have access to the order object.
If you already have the $order_id, you can simply perform the following code to get the order object:
$order = wc_get_order( $order_id );
If you don’t have the $order_id
If you don’t have the $order_id or object, you can access these by using the following code:
global $woocommerce, $post;
$order = new WC_Order($post->ID);
Accessing WooCommerce Order Details
Now that we have the $order object we can push on.
There are simple methods for accessing any order information, as outlined below.
Make sure you assign your order data to a variable.
/**
*
* Get Customer Related Data
*
*/
$order->get_address(); // Returns the requested address in raw, non-formatted way.
$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_company(); // Get billing company.
$order->get_billing_country(); // Get billing country.
$order->get_billing_email(); // Get billing email.
$order->get_billing_first_name(); // Get billing first name.
$order->get_billing_last_name(); // Get billing last name.
$order->get_billing_phone(); // Get billing phone.
$order->get_billing_postcode(); // Get billing postcode.
$order->get_billing_state(); // Get billing state.
$order->get_customer_id(); // Get customer_id.
$order->get_customer_ip_address(); // Get customer ip address.
$order->get_customer_note(); // Get customer note.
$order->get_customer_order_notes(); // List order notes (public) for the customer.
$order->get_customer_user_agent(); // Get customer user agent.
$order->get_formatted_billing_address(); // Get a formatted billing address for the order.
$order->get_formatted_billing_full_name(); // Get a formatted billing full name.
$order->get_formatted_shipping_address(); // Get a formatted shipping address for the order.
$order->get_formatted_shipping_full_name(); // Get a formatted shipping full name.
$order->get_shipping_address_1(); // Get shipping address line 1.
$order->get_shipping_address_2(); // Get shipping address line 2.
$order->get_shipping_address_map_url(); // Get a formatted shipping address for the order.
$order->get_shipping_city(); // Get shipping city.
$order->get_shipping_company(); // Get shipping company.
$order->get_shipping_country(); // Get shipping country.
$order->get_shipping_first_name(); // Get shipping first name.
$order->get_shipping_last_name(); // Get shipping_last_name.
$order->get_shipping_phone(); // Get shipping phone.
$order->get_shipping_postcode(); // Get shipping postcode.
$order->get_shipping_state(); // Get shipping state.
$order->get_user(); // Get the user associated with the order. False for guests.
$order->get_user_id(); // Alias for $order->get_customer_id();.
/**
*
* Get Order Totals
*
*/
$order->get_cart_tax(); // Gets cart tax amount.
$order->get_discount_tax(); // Get discount_tax.
$order->get_discount_to_display(); // Get the discount amount (formatted).
$order->get_discount_total(); // Get discount_total.
$order->get_formatted_line_subtotal(); // Gets line subtotal - formatted for display.
$order->get_formatted_order_total(); // Gets order total - formatted for display.
$order->get_item_subtotal(); // Get item subtotal - this is the cost before discount.
$order->get_item_tax(); // Get item tax - useful for gateways.
$order->get_item_total(); // Calculate item cost - useful for gateways.
$order->get_line_subtotal(); // Get line subtotal - this is the cost before discount.
$order->get_line_tax(); // Get line tax - useful for gateways.
$order->get_line_total(); // Calculate line total - useful for gateways.
$order->get_order_item_totals(); // Get totals for display on pages and in emails.
$order->get_prices_include_tax(); // Get prices_include_tax.
$order->get_rounded_items_total(); // Return rounded total based on settings. Will be used by Cart and Orders.
$order->get_shipping_tax(); // Get shipping_tax.
$order->get_shipping_total(); // Get shipping_total.
$order->get_subtotal(); // Gets order subtotal.
$order->get_subtotal_to_display(); // Gets subtotal - subtotal is shown before discounts, but with localised taxes.
$order->get_tax_totals(); // Get taxes, merged by code, formatted ready for output.
$order->get_taxes(); // Return an array of taxes within this order.
$order->get_total(); // Gets order grand total. incl. taxes. Used in gateways.
$order->get_total_discount(); // Gets the total discount amount.
$order->get_total_fees(); // Calculate fees for all line items.
$order->get_total_shipping(); // Gets shipping total. Alias of WC_Order::$order->get_shipping_total();.
$order->get_total_tax(); // Get total tax amount. Alias for $order->get_order_tax();.
// Refund related methods
$order->get_qty_refunded_for_item(); // Get the refunded amount for a line item.
$order->get_refunds(); // Get order refunds.
$order->get_remaining_refund_amount(); // How much money is left to refund?
$order->get_remaining_refund_items(); // How many items are left to refund?
$order->get_tax_refunded_for_item(); // Get the refunded tax amount for a line item.
$order->get_total_qty_refunded(); // Get the total number of items refunded.
$order->get_total_refunded(); // Get amount already refunded.
$order->get_total_refunded_for_item(); // Get the refunded amount for a line item.
$order->get_total_tax_refunded(); // Get the total tax refunded.
$order->get_total_tax_refunded_by_rate_id(); // Get total tax refunded by rate ID.
$order->get_total_shipping_refunded(); // Get the total shipping refunded.
/**
*
* Access Product Data
*
*/
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 object gives you access to all product data
$product_name = $item->get_name();
$quantity = $item->get_quantity();
$subtotal = $item->get_subtotal();
$total = $item->get_total();
$tax_subtotal = $item->get_subtotal_tax();
$tax_class = $item->get_tax_class();
$tax_status = $item->get_tax_status();
$all_meta_data = $item->get_meta_data();
$your_meta_data = $item->get_meta( '_your_meta_key', true );
$product_type = $item->get_type();
}
// Product related methods
$order->get_item(); // Get an order item object, based on its type.
$order->get_item_count(); // Gets the count of order items of a certain type.
$order->get_item_count_refunded(); // Gets the count of order items of a certain type that have been refunded.
$order->get_item_downloads(); // Get the downloadable files for an item in this order.
$order->get_item_meta(); // Get order item meta.
$order->get_item_meta_array(); // Get all item meta data in array format in the order it was saved. Does not group meta by key like $order->get_item_meta();.
$order->get_items(); // Return an array of items/products within this order.
$order->get_items_tax_classes(); // Get all tax classes for items in the order.
$order->get_product_from_item(); // Get a product (either product or variation).
/**
*
* Order Data
*
*/
$order->get_base_data(); // Get basic order data in array format.
$order->get_cancel_endpoint(); // Helper method to return the cancel endpoint.
$order->get_cancel_order_url(); // Generates a URL so that a customer can cancel their (unpaid - pending) order.
$order->get_cancel_order_url_raw(); // Generates a raw (unescaped) cancel-order URL for use by payment gateways.
$order->get_cart_hash(); // Get cart hash.
$order->get_changes(); // Expands the shipping and billing information in the changes array.
$order->get_checkout_order_received_url(); // Generates a URL for the thanks page (order received).
$order->get_checkout_payment_url(); // Generates a URL so that a customer can pay for their (unpaid - pending) order. Pass 'true' for the checkout version which doesn't offer gateway choices.
$order->get_coupon_codes(); // Get used coupon codes only.
$order->get_coupons(); // Return an array of coupons within this order.
$order->get_created_via(); // Get created via.
$order->get_currency(); // Gets order currency.
$order->get_data(); // Get all class data in array format.
$order->get_data_keys(); // Returns array of expected data keys for this object.
$order->get_data_store(); // Get the data store.
$order->get_date_completed(); // Get date completed.
$order->get_date_created(); // Get date_created.
$order->get_date_modified(); // Get date_modified.
$order->get_date_paid(); // Get date paid.
$order->get_download_url(); // Get the Download URL.
$order->get_downloadable_items(); // Get downloads from all line items for this order.
$order->get_edit_order_url(); // Get's the URL to edit the order in the backend.
$order->get_extra_data_keys(); // Returns all "extra" data keys for an object (for sub objects like product types).
$order->get_fees(); // Return an array of fees within this order.
$order->get_id(); // Returns the unique ID for this object.
$order->get_meta(); // Get Meta Data by Key.
$order->get_meta_cache_key(); // Helper method to compute meta cache key. Different from WP Meta cache key in that meta data cached using this key also contains meta_id column.
$order->get_meta_data(); // Get All Meta Data.
$order->get_object_read(); // Get object read property.
$order->get_order(); // Gets an order from the database.
$order->get_order_currency(); // Get currency.
$order->get_order_key(); // Get order key.
$order->get_order_number(); // Gets the order number for display (by default, order ID).
$order->get_parent_id(); // Get parent order ID.
$order->get_payment_method(); // Get the payment method.
$order->get_payment_method_title(); // Get payment method title.
$order->get_payment_tokens(); // Returns a list of all payment tokens associated with the current order
$order->get_shipping_method(); // Gets formatted shipping method title.
$order->get_shipping_methods(); // Return an array of shipping costs within this order.
$order->get_shipping_to_display(); // Gets shipping (formatted).
$order->get_status(); // Return the order statuses without wc- internal prefix.
$order->get_transaction_id(); // Get transaction d.
$order->get_type(); // Get internal type.
$order->get_used_coupons(); // Get coupon codes only.
$order->get_version(); // Get order_version.
Hello. $order->get_customer_id() returns 0 for an order not yet paid. What can I do?
Hi there, this wouldn’t be for an unpaid order, this would be for someone who doesn’t have an account. If it returns 0, then you are dealing with a “guest” user. To access the name / email address blah blah of an order access them directly from the order using $order->get_billing_email(); etc
Thanks, Chris