WooCommerce – Get Order Info (items, billing, shipping, etc) From $order Object


As a WooCommerce developer, every day I repeat many coding operations that make me waste my time.

For Example :

  • How to get Order ID?
  • How to get an order object?
  • How to get order details?
  • How to get shipping and billing info from $order Object?
  • How to get order Object?
  • How to get order total?
  • How to get order items?
  • How to get order shipping info (shipping details)?
  • How to get order billing info (billing details)?

Sometimes, you may have the $order_id available for example. In that scenario, you can “get” the order object with the wc_get_order WooCommerce function.

It’s also possible to gain $order information if you are in an email template. This can be helpful to show an additional $order information in your transactional communications or trigger custom functions. Either way, enjoy it!

1 ) Get access to $order variable

Hooks (do_action and apply_filters) use additional arguments which are passed on to the function. If they allow you to use the “$order” object you’re in business. Here’s how to get all the order information:

// Get Order ID and Key
// Get Order Totals $0.00
// 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();
   $name = $item->get_name();
   $quantity = $item->get_quantity();
   $subtotal = $item->get_subtotal();
   $total = $item->get_total();
   $tax = $item->get_subtotal_tax();
   $taxclass = $item->get_tax_class();
   $taxstat = $item->get_tax_status();
   $allmeta = $item->get_meta_data();
   $somemeta = $item->get_meta( '_whatever', true );
   $type = $item->get_type();
// Other Secondary Items Stuff
// Get Order Lines
// Get Order Shipping
// Get Order Dates
// Get Order User, Billing & Shipping Addresses
// Get Order Payment Details
// Get Order URLs
// Get Order Status

2 ) Get access to $order_id variable

If you have access to the order ID (once again, usually the do_action or apply_filters might give you this), you have to get the order object first. Then do the exact same things as above.

// Get $order object from order ID
$order = wc_get_order( $order_id );
// Now you have access to (see above)...
if ( $order )
$order->get_total(); //any other data related to order object

3. Get access to $email variable

If you are working with WooCommerce emails, often you will have the $email object available as parameter. In order to get the object from that, you need an additional step. Then do the exact same things as above.

// Get $order object from $email
$order = $email->object;
// Now you have access to (see above)...
if ( $order )
$order->get_formatted_order_total( );
// etc.

Leave a Reply

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