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.
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?):
and I am trying to get all kinds of stuff from about the Order, so I’ve started with the following:
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
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
hello,
I have used woocommerce_thankyou hook but still, am getting error ‘site blocked’.
No idea
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.
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!
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?
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!
use this snippet to get the all used coupon to this order.
Thanks!
Hello, I want to show the Order Status in the PDF Invoice, how can I do it?
get_status($order_status); ?>
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!
This is Very Helpful ….. Thanks alot
Great!
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
Edo, please ask their support team
Hi Rodolfo!
How can I have access in the $order object?
Hey Mike! On which page are you?
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?
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
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.
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
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
Upss! I forgot to wrap it between tags.
This is the URL:
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
Hey Rodolfo,
I tried to publish:
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.
Hey Marco, thanks for your comment! You need to pass “$order_id” to the function
Yeah, sure, but I don’t know how to do it… ๐
Could you help me?
Thanks!
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
Sure! I completely understand. Finally I got the solution.
Thanks for your help!
Awesome ๐
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.
Great Blogpost, Thanks Rodolfo.
๐