A Programmer’s Guide to Getting Order Info from the $order Object in WooCommerce

As a programmer, you may often find yourself working with the $order object in WooCommerce. This object contains information about the order, such as the customer’s name, the products ordered, and the shipping address. In this article, we’ll discuss how to get order info from the $order object, including different methods you can use to access specific pieces of information.

First, let’s take a look at the structure of the $order object. The $order object is an instance of the WC_Order class, which contains various properties and methods that you can use to access order information. Some of the most commonly used properties of the $order object include:

$order = wc_get_order($order_id);
 * Methods for getting data from the order object.
// Get the unique ID for this order.
// Get order key.
//Gets the order number for display (by default, order ID).
// Get basic order data in array format.
// Get parent order ID.
// Get all class data in array format.
// Get customer_id
// Get customer ip address.
// Get customer user agent.
// Get the user associated with the order. False for guests.
// Get billing first name.
// Get billing last name.
// Get a formatted billing full name.
// Get billing company.
// Get billing address line 1.
// Get billing address line 2.
// Get billing city.
// Get billing state.
// Get billing postcode.
// Get billing country.
// Get a formatted billing address for the order.
// Get billing email.
// Get billing phone.
// Get shipping first name.
// Get shipping last name.
// Get a formatted shipping full name.
// Get shipping company.
// Get shipping address line 1.
// Get shipping address line 2.
// Get shipping city.
// Get shipping state.
// Get shipping postcode.
// Get shipping country.
// Get a formatted shipping address for the order.
// Get shipping phone.
// Gets formatted shipping method title.
// Get the payment method.
// Get the payment method title.
// Get transaction d.
// Get created via.
// Get customer note.
// Get date completed.
// Get date paid.
// Get cart hash.
// Gets the requested address in raw, non-formatted way.
// Get a formatted shipping address for the order.
// Gets information about whether stock was reduced.
// Gets information about whether permissions were generated yet.
// Whether email have been sent for this order.
// Gets information about whether sales were recorded.
// Get downloads from all line items for this order.
 *  URLs and Endpoints
// 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.
// Generates a URL for the thanks page (order received)
// Generates a URL so that a customer can cancel their (unpaid - pending) order.
// Generates a URL to view an order from my account page.
// Gets the URL to edit the order in the backend.
// List order notes (public) for the customer.
 *  Refunds
// Get order refunds.
// Get amount already refunded.
// Get the total tax refunded.
// Get the total shipping refunded.
// Gets the count of order items of a certain type that have been refunded.
// Get the total number of items refunded.
// Get the refunded amount for a line item.
// Get the refunded amount for a line item.
// Get the refunded tax amount for a line item.
$order->get_tax_refunded_for_item($item_id, $tax_id);
// Get total tax refunded by rate ID.
// How much money is left to refund?
// How many items are left to refund?
// Get totals for display on pages and in emails.
 *  Prices
// Gets order currency.
// Get prices_include_tax.
// G Get discount_total.
// Get discount_total.
// Get shipping_total.
// Get shipping_tax.
// Gets order grand total including taxes, shipping cost, fees, and coupon discounts. Used in gateways.
// Get total tax amount. Alias for get_order_tax().
// Gets the total discount amount.
// Gets order subtotal. Order subtotal is the price of all items excluding taxes, fees, shipping cost, and coupon discounts.
// If sale price is set on an item, the subtotal will include this sale discount. E.g. a product with a regular
// price of $100 bought at a 50% discount will represent $50 of the subtotal for the order.
// Get taxes, merged by code, formatted ready for output.

// Return an array of fees within this order.
// Return an array of taxes within this order.
// Return an array of coupons within this order.
// Get used coupon codes only.
// Gets the count of order items of a certain type.

// Get date_created.
// Get date_modified.
// Get date_paid.
// Get date_modified.
// Get the order statuses without wc- internal prefix.

 *  Order Items
foreach ( $order->get_items() as $item_id => $item ) {
	//Get order ID this meta belongs to.
	// Get parent order object.
	// Get order item name.
	// Get order item type. Overridden by child classes.
	// Get the associated product.
	// Get product ID.
	//  Get variation ID.
	// Get quantity
	// Get tax status.
	//Get tax class.
	// Get total.
	// Get total tax.
	//Get subtotal.
	// Get subtotal tax.
	// Get All Meta Data.
	// Get custom meta data
	$item->get_meta( '_custom', true );

Leave a Comment

Your email address will not be published. Required fields are marked *