WooCommerce: Decrease Product ‘Total Sales’ Upon Refund

Every time an order is placed on your WooCommerce website, the purchased products’ total_sales counter increases thanks to a core function. It’s then easy to retrieve the value and maybe show the number of sales on the single product page, which is great for social proof and sales conversion rate optimization.

The big problem – and don’t ask me why this is not into core – is that if you need to refund an order (or cancel it), the total_sales counter will not reset / decrease, which is odd.

Today we’ll fix this, so that your products are always up to date with their sales counter and you can be more confident in showing the correct values. Enjoy!

Continue reading WooCommerce: Decrease Product ‘Total Sales’ Upon Refund

WooCommerce: Get “Trending” Products

There is a way to display the WooCommerce bestsellers via shortcode / block, but there is no way to calculate the bestsellers – say – from the last 7 days i.e. a list of “trending” products over the last N days.

The function below gives you a way to loop through the latest orders and fill an array with the top 10 products – you can then pass this list of IDs to a shortcode to display them wherever you wish. Enjoy!

Continue reading WooCommerce: Get “Trending” Products

WooCommerce: Count Variation Sales (Shortcode)

We’ve already studied how to display the number of sales for a given product ID via a shortcode – however that solution won’t work for a variation ID, because WooCommerce only counts the “parent product” sales.

We need a different workaround in this case. This will require we either query the orders that contain such variation ID, and then calculate the sum – or that we install a snippet on day 0 so that we can count variation sales from that moment onwards, without having to query and calculate anything.

We will study the latter, and then display the result via a shortcode, so that you can use it anywhere, even inside the variation description.

Enjoy!

Continue reading WooCommerce: Count Variation Sales (Shortcode)

WooCommerce: Limit Sales Of A Product Per Day

Yes, “manage stock” is a nice feature to make sure you don’t oversell a given product based on the stock you have in your warehouse. However, what if you also need to have a “daily sales limit” – say you can’t sell more than 3 of a given product ID in a given day?

This is an interesting functionality that is also helpful for you to learn how to get today’s orders, how to loop through the orders to find a specific product ID and sum its quantities, and finally how to use the woocommerce_is_purchasable filter to set if a product can be purchased or not (which means, the add to cart may or may not show). Enjoy!

Continue reading WooCommerce: Limit Sales Of A Product Per Day

WooCommerce: Calculate Sales by State

You’re filing your tax returns and need to know how much you earned in each state… but then find out WooCommerce doesn’t give you this calculation by default within its reports!

Don’t worry – today I’ll share a quick snippet so that you can calculate the amount you need in a second. Feel free to change the year, the country and the states in the snippet.

Continue reading WooCommerce: Calculate Sales by State