When I am working on WoCoomerce getting aroung with $order object is really hard and remember all functions associated with it. As a WooCommerce developer, I search for common question when I am working on cart, check, emails, or account pages to handle order details.
I do a lot of search for how to get ___ from $order Object? Some of my commong questions are
- How to get order taxes?
- How to Get order products?
- How to get order customer ID?
- How to get billing information? and So On…
My primary purpose to write this article is to explore $order methods. We can get $order method using following ways.
1. Access to $order Variable
Here’s how to get all the order information:
<?php
// 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) {
// Retrieve information about each order 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();
$allmeta = $item->get_meta_data();
$somemeta = $item->get_meta('_meta_key', 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();
// ... (Other billing and shipping address-related methods)
// 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();
PHP2. Have access to $order_id variable
If you have access to $order_id variable, you can get $order object using function wc_get_order($order_id). Then you would have access to methods associated with
<?php
// How to get $order object from order ID
// wc_get_order provide a standard way of retrieving orders that is safe to use and will not break due to database changes in future WooCommerce versions.
$order = wc_get_order( $order_id );
// Now you have access to $order and now use have use above discussed methods
if ( $order ) {
$order->get_formatted_order_total( );
// and so on
}
PHP3. Have access to $email variable
When we are working with emails, we have access to $email Object. Following trick will give you the access to $order from that you can dig into deeper.
// How to get $order object from $email
$order = $email->object;
// Now you have access to $order and now use have use above discussed methods
if ( $order ) {
$order->get_id();
$order->get_formatted_order_total( );
// etc.
// etc.
}
PHP