WooCommerce: Calculate Sales by State

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.

WooCommerce: get sales by state

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>";


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.

19 thoughts on “WooCommerce: Calculate Sales by State

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

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

  2. This code works but this is slower version. Doesn’t work with huge data. Better if this code written for Mysql Query.

    1. Ok thank you!

  3. 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! ๐Ÿ™‚

    1. Thank you!

    2. Agreed Dave, I would absolutely pay for a plugin that did this.

  4. 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?

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

  5. 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 ?


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

  6. 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 http://prntscr.com/gnejer

    Can you direct me how to change? thanks

    1. Cristian, thanks for your comment! I believe I already helped you via Bloomer Armada support. Hope you managed to fix it ๐Ÿ™‚

  7. Works great! Thanks. How about a list of 10 states with most sales?

    1. Thank you ๐Ÿ™‚ Sure, that can be done, you will need to do some PHP edits ๐Ÿ™‚

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

  9. Really nice excellent tutorial

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 *