WooCommerce: Get Total Sales By Product Category

It’s easy enough in WooCommerce to get/calculate product sales. What’s difficult, on the other hand, is calculating the total amount of sales for a specific category, because there is no core function that already does that.

Why sales by category – you may ask? Well, to me, that’s a very important metric. For example, I sell both consulting and non-consulting products on this same website, so it’s important for me to keep track of category sales year-on-year, especially when my goal is reducing 1-to-1 client work while increasing scalable product sales such as courses, plugins and memberships.

In this quick tutorial, we will first get the “WooCommerce orders that contain a target product category”, and after that we will loop through the array to calculate the total sales for that specific category. Sounds difficult? No worries – just copy and paste the snippets below.

Continue reading WooCommerce: Get Total Sales By Product Category

WooCommerce: Update Self-Hosted Plugin @ WP Dashboard

In this million-dollar tutorial you will learn how to update a custom (WooCommerce) plugin that you host somewhere, directly from the WordPress dashboard.

I thought the WordPress dashboard could only notify you of plugin updates and let you exclusively update plugins that are on the WordPress repository, but I was wrong!

Since I started selling WooCommerce plugins here on Business Bloomer, I had to find a way to let customers update them automatically right from their WP admin.

Thankfully, there are 2 hooks that come to the rescue: pre_set_site_transient_update_plugins update_plugins_{$hostname} and plugins_api. With these two filters, you can tell WordPress that your custom plugin ZIP file is downloadable at a given public URL, show a notification to the customer that a plugin update is available, let them update with 1 click, and optionally let them enable auto-updates.

So, let’s see how I run my plugin business. Enjoy!

Continue reading WooCommerce: Update Self-Hosted Plugin @ WP Dashboard

WooCommerce: Email Admin Upon Fatal Error

WooCommerce has a nice feature when it comes to WordPress Error 500 / Fatal Error – it logs the error and all the information regarding it inside the WooCommerce Status > Logs > Fatal Errors area.

My problem is that sometimes these errors occur in the backend, so they may not trigger the WordPress built-in email that notifies the admin about the problem.

What I want to try (please test it on your development website first, and not on your live website), is a way to get an email each time WooCommerce logs an error, so that I can go in and fix it immediately. Enjoy!

Continue reading WooCommerce: Email Admin Upon Fatal Error

WooCommerce: Disable Checkout Field Autocomplete

By default, WooCommerce adds the “autocomplete” attribute to almost all checkout fields. For example, “billing_phone” has “autocomplete=tel”, “billing_country” has “autocomplete=country” and so on.

When logged out or if the logged in user has never done a purchase before, the WooCommerce Checkout page fields are possibly autofilled by the browser based on saved data / addresses.

Today, we’ll take a look at how to disable this autofill behavior, so that the customer is forced to enter data inside an empty input, and maybe in this way you can apply your custom validation or pattern, such as a specific phone number format. Enjoy!

Continue reading WooCommerce: Disable Checkout Field Autocomplete

WooCommerce: Get EU, EU VAT, EEA, SCA Countries

When you deal with conditional payment gateways, or conditional notifications, or conditional content, you may need to use to check the current billing country against an array of countries (in plain English, you may want to know if the customer is based in a specific World area), such as the European Union.

Thankfully, WooCommerce gives us a nice prebuilt function so that we don’t need to create the array of EU countries manually. On top of that, it’s easy to manipulate that array in order to add / remove countries for different uses, such as getting the list of EEA (European Economic Area) countries or SCA (Strong Customer Authentication) countries. In this post, I will give you some shortcuts to “calculate” these arrays, so that you don’t need to bother doing it.

Please note, EU, EEA and SCA countries are constantly changing, so these arrays are updated to the date you see on top of this post. Enjoy!

Continue reading WooCommerce: Get EU, EU VAT, EEA, SCA Countries

WooCommerce: Add Product To Order After Purchase

On Business Bloomer I sell a bundle of products, and I use no Bundles plugin for that. So the challenge was to programmatically add a list of products to the order upon purchase, once the bundle product is purchased.

This is an amazing way to save time for the customer, as they don’t need to manually add each product to the cart. In the background, after a successful purchase, some magic code (that you find below) adds products to the order, sets their price to $0.00 (so that the order total is not altered), and saves the order. Enjoy!

Continue reading WooCommerce: Add Product To Order After Purchase

WooCommerce: Fix Google Search Console “No global identifier provided” Error

If you registered your WooCommerce website on Google Search Console for monitoring your SEO efforts and search appearance errors, you probably got this “No global identifier provided (e.g. gtin, brand)” email notification at some stage. I got it too.

Search Console optionally requests you set a unique product GTIN structured data for all your products – I believe in case you wish to sell on Google Shopping – and therefore sends you this error notification whenever a product is missing this.

You could use a WooCommerce GTIN plugin from the WP repo, yes. Or you could be smart, and programmatically set the GTIN to the same value of the product SKU, as long as all your products have a unique SKU value. Today, we will cover the latter. Enjoy!

Continue reading WooCommerce: Fix Google Search Console “No global identifier provided” Error

WooCommerce: Set Product Price Based On Other Products!

I’m Italian and I love exclamation marks! I also love WooCommerce customization, as you may know. This time I want to show you how I programmatically define the price of my WooCommerce Mini-Plugin All-Access-Bundle product… based on other products.

The backstory: as of today, I sell 18 WooCommerce plugins, and soon I should reach the 400 mark if all goes well. So, I came up with the idea of creating a bundle, and let customers gain access to all of them within a single, discounted purchase.

Yes, I could have purchased a Product Bundle plugin… but I wanted to see if I could create a bundle out of a Simple product.

The only requirements were: set the regular price based on the total price of the plugins, set its sale price based on a percentage discount, automate this so I don’t need to manually update the bundle price every time I add a new product, and add all plugin products to the order upon bundle purchase (we will see this in another snippet). Enjoy!

Continue reading WooCommerce: Set Product Price Based On Other Products!

WooCommerce: Display Sale Price End Date @ Shop & Single Product Page

WooCommerce allows you to “schedule” the product sale price – you can define a start date and an end date, so that you can run your promotion automatically.

However, for some reason, this information is only visible to the admin. It would be awesome to show the “sale price end date” to customers as well, don’t you think? So, let’s do it!

Continue reading WooCommerce: Display Sale Price End Date @ Shop & Single Product Page

WooCommerce: Get Variation IDs By Product Category or Tag

We can get products belonging to a product category or product tag very easily. What’s much more difficult is getting single variations based on a product category or product tag, as variations… don’t have a category or tag, which belong to its “parent” variable product instead.

In this post, we will define a function that you can reuse in your custom code whenever you need to get all variation IDs based on a given product category or product tag. You can then loop through the array and return whatever info you need. Enjoy!

Continue reading WooCommerce: Get Variation IDs By Product Category or Tag

WooCommerce: Get Draft Product URL

Now, this is a bit of a niche tutorial, but it may come useful you need to get a product permalink when it’s still a draft. As you can see from the screenshot below, this is helpful when the product URL is very long – in such a case WordPress truncates the “Permalink” string and you can’t really copy/paste.

For example, you may need the product URL for a scheduled blog post, a future newsletter, an email; in whatever case, this is a cool way to “get” the product URL and print it in the “Publish” meta box of the single product edit page. Enjoy!

Continue reading WooCommerce: Get Draft Product URL

WooCommerce: Add New Tab @ Single Product Page

I find it unusual that after years of sharing tutorials on Business Bloomer there is no snippet for creating a custom product tab on the single product page!

I mean, we’ve seen an example about Related Products in a Custom Tab snippet a while ago, but here I want to concentrate on the actual functionality of adding a custom tab, giving a name and a heading to it, showing it conditionally (e.g. only for a specific product category), and making sure the “scroll to tab” works when the URL contains its anchor tag.

Enjoy!

Continue reading WooCommerce: Add New Tab @ Single Product Page

WooCommerce: Move Sale Badge Beside Price @ Single Product Page

On a default theme, such as Storefront, the single product page template is laid out so that the SALE badge comes first, on its own line, then comes the product title, the product price on its own line, the short description, the product’s stock availability and the add to cart button.

As you can see from the screenshot below, there is lots of white space on the right hand side, so for my new business line (WooCommerce Mini-Plugins, you somewhat guessed it, right?) I wanted to improve the single product page layout and move the SALE badge right beside the price, so that I could achieve two objectives: save some space, and also focus the customer attention on the product price as opposed on to the badge.

So, let’s see how I did it. Enjoy!

Continue reading WooCommerce: Move Sale Badge Beside Price @ Single Product Page

WooCommerce: Product Description in a Shortcode!

It may be helpful to show the long product description of a given product in a custom WordPress page or post, so that you can use it as a teaser or anyway as a way to save some time instead of rewriting the whole thing.

The solution is easy: let’s code a simple shortcode that accepts a product ID as a shortcode attribute, and that returns its long description, properly formatted.

You can see this in action on this same website. Here’s one of my downloadable products’ long description: https://www.businessbloomer.com/shop/plugins/woocommerce-disable-payments-by-category/#tab-description – and here’s the same exact description printed in a blog post, thanks to the shortcode snippet you find below: https://www.businessbloomer.com/woocommerce-disable-payment-method-for-specific-category/#mini-plugin-business-bloomer-woocommerce-toggle-payments-by-category

Enjoy!

Continue reading WooCommerce: Product Description in a Shortcode!

WooCommerce: Disable Orders Table Row Hyperlink @ Admin

If you access the WordPress dashboard and go to WooCommerce > Orders, you will be presented with the WooCommerce “Orders Table” i.e. the list of customer orders. Each row comes with several columns, such as the order number, order date, order status, order billing and shipping addresses and more.

What’s annoying sometimes is that the whole row is a clickable link, so if you need to copy and paste some information you will end up actually clicking on it, and being redirected to the single order page.

There is a way to add a specific “class” to each table row, so that it becomes not clickable, and the only link that remains is the very first column table cell – the order number (as it should be by default!).

So, let’s see how easy it is to alter this default behaviour. Enjoy!

Continue reading WooCommerce: Disable Orders Table Row Hyperlink @ Admin

WooCommerce: Change Address Format For a Specific Country

We’ve already covered a little about address formats when we added a custom House Number field to the WooCommerce Checkout page.

Address formats define the way Billing and Shipping addresses are formatted for the end user. Addresses show on the order received / thank you page, on transactional emails and on some My Account pages, and look like this:

{name}\n{company}\n{address_1}\n{address_2}\n{city}\n{state}\n{postcode}\n{country}

Now, what if you wanted to move the “company” before the customer name, or maybe have “address 1” and “address 2” on the same line, or move “postcode” to the last line? Well, thankfully there is a neat WooCommerce filter called “woocommerce_localisation_address_formats” that allows us to do just that – reformat any address for any country we wish. Enjoy!

Continue reading WooCommerce: Change Address Format For a Specific Country

WooCommerce: Change “Buy Product” Label for External Products

By default, WooCommerce displays a “Buy Product” button label for external products. This button redirects to the external URL that is entered via the single product settings.

As usual, this “Buy Product” label may not suit all businesses, and therefore WooCommerce gives you the option to rename such buttons via the single product edit page settings. This is great, but at the same time you don’t want to manually edit hundreds of products when you can use a few lines of PHP, right?

So, here’s a super quick fix to override the “Buy Product” external add to cart button label to whatever you wish, without ever touching the manual settings. Enjoy!

Continue reading WooCommerce: Change “Buy Product” Label for External Products

WooCommerce: Refresh Checkout Upon Any Input Field Change

You may be aware that the WooCommerce Checkout page “order review” section reloads/refreshes every time there is a change of address – in this way shipping, taxes and optional fees are properly recalculated and customers are returned with the correct total.

But in case you add custom checkout fields that may cause a difference in pricing, or you want to keep the checkout UX consistent, or you have some other kinds of logic, it may be useful to refresh the order review section after a change on any checkout field. Enjoy!

Continue reading WooCommerce: Refresh Checkout Upon Any Input Field Change