You’re filling out your tax reports… and then find out WooCommerce doesn’t give you this calculation by default!
Don’t cry ๐ Today I’ll show you a quick snippet to calculate that in a second. Feel free to change the year, the country and the states in the snippet.

PHP Snippet: Get Sales by State @ WooCommerce Admin (Reports)
/** * @snippet Get Sales by State @ WooCommerce Admin * @how-to Get CustomizeWoo.com FREE * @sourcecode https://businessbloomer.com/?p=72853 * @author Rodolfo Melogli * @testedwith WooCommerce 3.1.2 */ // ----------------------- // 1. Create extra tab under Reports / Orders add_filter( 'woocommerce_admin_reports', 'bbloomer_admin_add_report_orders_tab' ); function bbloomer_admin_add_report_orders_tab( $reports ) { $array = array( 'sales_by_state' => array( 'title' => 'Sales by state', 'description' => '', 'hide_title' => 1, 'callback' => 'bbloomer_yearly_sales_by_state' ) ); $reports['orders']['reports'] = array_merge($reports['orders']['reports'],$array); return $reports; } // ----------------------- // 2. Calculate sales by state function bbloomer_yearly_sales_by_state() { $year = 2017; // change this if needed $total_ca = $total_wa = 0; // add states if needed $args = [ 'post_type' => 'shop_order', 'posts_per_page' => '-1', 'year' => $year, 'post_status' => ['wc-completed'] ]; $my_query = new WP_Query($args); $orders = $my_query->posts; foreach ($orders as $order => $value) { $order_id = $value->ID; $order = wc_get_order($order_id); $order_data = $order->get_data(); if ( $order_data['billing']['country'] === 'US' ) { if ( $order_data['billing']['state'] === 'CA' ) $total_ca += $order->get_total(); if ( $order_data['billing']['state'] === 'WA' ) $total_wa += $order->get_total(); } } echo "<h3>Sales by State for Year " . $year . "</h3>"; echo "CA: " . wc_price($total_ca) . "</br>"; echo "WA: " . wc_price($total_wa) . "</br>"; }
Hello. Does this code snippet still work now that WooCommerce has done away with the Reports section and now uses the Analytics section? Thanks!
Works for me – I have WooCommerce Analytics disabled lol!
Don’t know how everyone got it to work. As a newbie, I don’t know where to put this code. I followed the video link which led me to an account creation page. Added details, got an email created logged in, and reset the password. Now What? I can see that there is an order created but cannot open that video. If I come back here and click the video link again, It is asking again to create an account.
I am sure the provided code works very well but it is of no use for a newbie.
BTW I am not looking for pointers on where to put the code, I am just bringing up the workflow of this article/site.
Thanks for your feedback! I’ve revised the sign-up process, try again and let me know
Is it possible to add Sales by State as the first tab? Before Sales by date?
Hi, 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!
I updated Fred D.’s code to include some line breaks for list of states, and also to edit the html code that got inserted in his post.
Nice!
Cool, thank you!
I updated it a bit to make it dynamically build out the states. Could modify a bit more to automatically do the countries too, but this was all I needed for now. Hope it helps:
Awesome! All your HTML chars have been decoded and show like “>” for example. If you want to send a revision, feel free to do so
For anyone wanting to use this code, there are some HTML entity references that were triggering a fatal error on my site. Run a search and replace for [php]>[php] and change it to [php]>[php] and this snippet will work perfectly.
Ok, my comment above got auto-corrected. The HTML entity you want to replace with “>” is “>”
Thank you. Also https://www.businessbloomer.com/woocommerce-calculate-sales-state/#comment-348485
It works great for me for all years except 2019! I don’t fully understand why, I can parse data for 2018 and 2020 but not for 2019. I get the following error: “Fatal error: Allowed memory size of 805306368 bytes exhausted (tried to allocate 20480 bytes) in /home/dermapla/public_html/wp-includes/wp-db.php on line 1995”
Just a heads up. I do appreciate the snippet though!
I suppose I will try to find a different solution.
I guess you have too many orders in 2019 and the snippet crashes. It is definitely possible to fix this in regard to performance, 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 code works but this is slower version. Doesn’t work with huge data. Better if this code written for Mysql Query.
Ok thank you!
Awesome. This helped beyond measure!
Took some time to add all 50 states to the array but otherwise, was excellent. If you do ever build a plugin for this setup so that users could just install and enter the date range, I would pay for that! ๐
Thank you!
the following line generates an error now in the current WooCommerce Orders page:
Error reads:
Line 135 is where it sits in my child theme’s functions.php file of course. From what I can tell, it is not creating any issues anywhere else.
Not sure if this is due to the fact WooCommerce discontinued their reports admin. If you find a fix let me know!
I have the same issue. The extra report option causes the array_merge error and I have been unable to figure out how to correct this. Any ideas? Love the snippet otherwise!!
Agreed Dave, I would absolutely pay for a plugin that did this.
This is so helpful. Thank you! I have four states that I needed to pull information for, and this did the trick!
How difficult would it be to display by month within the year totals? For instance, 2017 is already showing for the year – then filter down by month within the state totals like so:
WA > Jan – $xxx; Feb – $xxx; Mar – $xxx | CA > Jan – $xxx; Feb – $xxx; Mar – $xxx and so on?
Karen, thanks so much for your comment! Yes, this is totally possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Hello, I am using Woocommerce Brand Plugin.
Plugin Link: https://woocommerce.com/products/brands/
I want to Get Sales by Brand Report. How can I do this ?
Regards
James, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R
Cool Idea!
What if i need to make this for Romania? Should i put RO to states?
Because now it gives me an error.
We put Romanian states using a plugin, but your snippet doesnt seem to work with that states https://prntscr.com/gnejer
Can you direct me how to change? thanks
Cristian, thanks for your comment! I believe I already helped you via Bloomer Armada support. Hope you managed to fix it ๐
Works great! Thanks. How about a list of 10 states with most sales?
Thank you ๐ Sure, that can be done, you will need to do some PHP edits ๐
Every morning I wake-up I look forward to seeing your e-mail. Another awesome PHP snippet I’ve already added all the states and they work fabulously. Great work.
Awesome!!!
Really nice excellent tutorial
Thank you ๐