Posted on

WooCommerce: Easily Get Order Info (total, items, etc) from $order Object

As WooCommerce a freelancer, every day I repeat many coding operations that make me waste time. One of them is: “How to get ____ if I have the $order variable/object?”.

For example, “How can I get the order total”? Or “How can I get the order items”? Or maybe the order ID, customer ID, billing info, payment method, total refunds and so on… hopefully this article will help you save time as well ๐Ÿ™‚

1. You have access to $order

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
$order->get_id()

// Get Order Totals $0.00
$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_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( string $item_type = '' )
$order->get_total_qty_refunded( string $item_type = 'line_item' )
$order->get_qty_refunded_for_item( integer $item_id, string $item_type = 'line_item' )
$order->get_total_refunded_for_item( integer $item_id, string $item_type = 'line_item' )
$order->get_tax_refunded_for_item( integer $item_id, integer $tax_id, string $item_type = 'line_item' )
$order->get_total_tax_refunded_by_rate_id( integer $rate_id )
$order->get_remaining_refund_amount( )

// Get Order Items
$order->get_items()
$order->get_items_key()
$order->get_items_tax_classes()
$order->get_item()
$order->get_item_count()
$order->get_item_subtotal()
$order->get_item_tax()
$order->get_item_total()
$order->get_downloadable_items( )

// 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( string $context = 'view'  )
$order->get_date_paid( string $context = 'view'  )

// Get Order User, Billing & Shipping Addresses
$order->get_customer_id( string $context = 'view'  )
$order->get_user_id( string $context = 'view'  )
$order->get_user( )
$order->get_customer_ip_address( string $context = 'view'  )
$order->get_customer_user_agent( string $context = 'view'  )
$order->get_created_via( string $context = 'view'  )
$order->get_customer_note( string $context = 'view'  )
$order->get_address_prop( string $prop, string $address = 'billing', string $context = 'view'  )
$order->get_billing_first_name( string $context = 'view'  )
$order->get_billing_last_name( string $context = 'view'  )
$order->get_billing_company( string $context = 'view'  )
$order->get_billing_address_1( string $context = 'view'  )
$order->get_billing_address_2( string $context = 'view'  )
$order->get_billing_city( string $context = 'view'  )
$order->get_billing_state( string $context = 'view'  )
$order->get_billing_postcode( string $context = 'view'  )
$order->get_billing_country( string $context = 'view'  )
$order->get_billing_email( string $context = 'view'  )
$order->get_billing_phone( string $context = 'view'  )
$order->get_shipping_first_name( string $context = 'view'  )
$order->get_shipping_last_name( string $context = 'view'  )
$order->get_shipping_company( string $context = 'view'  )
$order->get_shipping_address_1( string $context = 'view'  )
$order->get_shipping_address_2( string $context = 'view'  )
$order->get_shipping_city( string $context = 'view'  )
$order->get_shipping_state( string $context = 'view'  )
$order->get_shipping_postcode( string $context = 'view'  )
$order->get_shipping_country( string $context = 'view'  )
$order->get_address( string $type = 'billing'  )
$order->get_shipping_address_map_url( )
$order->get_formatted_billing_full_name( )
$order->get_formatted_shipping_full_name( )
$order->get_formatted_billing_address( string $empty_content = ''  )
$order->get_formatted_shipping_address( string $empty_content = ''  )

// Get Order Payment Details
$order->get_payment_method( string $context = 'view'  )
$order->get_payment_method_title( string $context = 'view'  )
$order->get_transaction_id( string $context = 'view'  )

// Get Order URLs
$order->get_checkout_payment_url( boolean $on_checkout = false  )
$order->get_checkout_order_received_url( )
$order->get_cancel_order_url( string $redirect = ''  )
$order->get_cancel_order_url_raw( string $redirect = ''  )
$order->get_cancel_endpoint( )
$order->get_view_order_url( )
$order->get_edit_order_url( )

// Get Order Status
$order->get_status( )

// source: https://docs.woocommerce.com/wc-apidocs/class-WC_Order.html

2. You have access to $order_id

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)...

$order->get_id()
$order->get_formatted_order_total( )
// etc.
// etc.

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

13 thoughts on “WooCommerce: Easily Get Order Info (total, items, etc) from $order Object

  1. Hello Rodolfo,

    I have created a custom template in my theme file and would to get Woocommerce order details variable in that page. However when I tried to echo $order->get_total(), internet server error with notice “Failed to load resource” appeared. Is there possibility whereby the code only function if the php is in Woocommerce plugin directory?

    Thank you.

    1. Hey Will, thanks for your comment! The code will work everywhere (however, it’s better if you place it in functions.php) as long as you have access to the $order variable

  2. Hi Rodolfo,

    I would like to add a script at the “thank you page” in WooCommerce to run a URL like:

    Could you help me getting this parameters?

    Thank you

    1. Upss! I forgot to wrap it between tags.

      This is the URL:

      <script async src="https://example.com?total_price=XXXXXXX&order_id=XXXXXXX"></script>
    2. Hey Marco, thanks for your comment! Please see https://businessbloomer.com/woocommerce-add-conversion-tracking-code-thank-page/ and then given you have access to “$order_id”, you can “echo” those values with PHP inside the script. Hope this helps

    3. Hey Rodolfo,

      I tried to publish:

      add_action( 'woocommerce_thankyou', 'bbloomer_conversion_tracking_thank_you_page' );
       
      function bbloomer_conversion_tracking_thank_you_page() {
      
          $order = wc_get_order( $order_id );
      
          echo $order->get_order_number();
          echo $order->get_formatted_order_total();
      
      ?>
      
      <?php
      }
      

      But the page seems blocked. When I remove the code above and I reload the page, then it works. I think that the problem is related to de $order. I am not a developer.

      Thank you very much for your help.

    4. Hey Marco, thanks for your comment! You need to pass “$order_id” to the function

    5. Yeah, sure, but I don’t know how to do it… ๐Ÿ™
      Could you help me?

      Thanks!

    6. Marco unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

    7. Sure! I completely understand. Finally I got the solution.

      Thanks for your help!

    8. Awesome ๐Ÿ™‚

  3. Great Blogpost, Thanks Rodolfo.

Questions? Feedback? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.