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();
$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 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();
$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();
$order->get_billing_first_name();
$order->get_billing_last_name();
$order->get_billing_company();
$order->get_billing_address_1();
$order->get_billing_address_2();
$order->get_billing_city();
$order->get_billing_state();
$order->get_billing_postcode();
$order->get_billing_country();
$order->get_billing_email();
$order->get_billing_phone();
$order->get_shipping_first_name();
$order->get_shipping_last_name();
$order->get_shipping_company();
$order->get_shipping_address_1();
$order->get_shipping_address_2();
$order->get_shipping_city();
$order->get_shipping_state();
$order->get_shipping_postcode();
$order->get_shipping_country();
$order->get_address();
$order->get_shipping_address_map_url();
$order->get_formatted_billing_full_name();
$order->get_formatted_shipping_full_name();
$order->get_formatted_billing_address();
$order->get_formatted_shipping_address();

// 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();

// 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 snippet?

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 files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

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 Customization?

Check out these free video tutorials. You can start learning how to customize WooCommerce without unnecessary plugins. Watch me code and learn by example!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

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

  1. Thanks for this list! I’m trying to make good use of it. So I’ve set up an action in my functions.php file like this (is this a good place to hook into Woo?):

    add_action( 'woocommerce_before_checkout_billing_form', 'bazaarvoice_data' );

    and I am trying to get all kinds of stuff from about the Order, so I’ve started with the following:

    function bazaarvoice_data () {
    
    global $woocommerce;
    global $order_id, $order;
    
    $order_id = $order->get_id();
    
    echo $order_id;
    
    }
    

    And, when I run things, I keep getting this error:
    [22-Sep-2019 22:58:45 UTC] PHP Fatal error: Call to a member function get_id() on string in /mnt/objectivefs/dev_sites/gunvault/wp-content/themes/Avada-Child-Theme/functions.php on line 50

    So, I figure I’m missing something, but what? Do I need to add something to the get_id()? Or is it something else?

    Huge thanks in advance.

    ~Walt

    1. Hi Walt – that’s the wrong hook as the $order doesn’t exist yet. Try with “woocommerce_thankyou” instead, which gives you access to $order_id

      1. hello,

        I have used woocommerce_thankyou hook but still, am getting error ‘site blocked’.

        1. No idea

  2. How do I display the number of orders and the number of downloads per user in user profile?
    I want to show the number of orders and number of download per user in user profile.

    1. Shahoo, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Hi,

    I used a total different template for those emails and unfortunately, I don’t manage to get the total discount…
    From this list (awesome btw), I should use either `$order->get_discount_total();` or `$order->get_total_discount();` but both display 0 while if I come back to the default woocommerce email template, the discount is displayed.

    The discount I’m talking about is not a coupon but a buy 2 get 1 free that has been coded in the store. But since Woocommerce template gets it, why can’t it?

    Thanks for the help,

    Any idea?

    1. Hello Nicolas, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  4. $order->get_used_coupons()

    use this snippet to get the all used coupon to this order.

    1. Thanks!

  5. Hello, I want to show the Order Status in the PDF Invoice, how can I do it?

    get_status($order_status); ?>

    1. Hello Mergim, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. This is Very Helpful ….. Thanks alot

    1. Great!

  7. Hi expert,

    Regards.
    I am using Woocommerce Multi Vendor Market Place (bu WCLovers) along with its WooCommerce Frontend Manager.
    I want to add more orders fields in Vendor Dashboard. In a view of Order List, like Billing Address for example.
    Please assist.

    Thank you
    Edo

    1. Edo, please ask their support team

  8. Hi Rodolfo!

    How can I have access in the $order object?

    1. Hey Mike! On which page are you?

  9. Can you help me ? Of course, for a fee. I want to add data to the order confirmation – everything is OK, but they are not showing off. Will you do such a service for me?

    1. Hello Jacek, thanks so much for your comment! Yes, if you’d like to get a quote, feel free to contact me here. Thanks a lot! ~R

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

  11. 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>
      1. 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

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

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

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

      Thanks!

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

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

      Thanks for your help!

      1. Awesome ๐Ÿ™‚

    5. Marosseculi, if you’re still here would you mind sharing how you got the order_id and order_total into the link? I understand where to put the code (functions.php) but I’m unsure of how those values get written into the link.

  12. Great Blogpost, Thanks Rodolfo.

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

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