Variable Products – Page 2

How to Add Custom Fields to Variations in WooCommerce

Some time ago, I published a tutorial about adding custom fields to products. I covered variable products there; however, I didn’t cover adding custom fields specifically to variations of variable WooCommerce products.

Now, I decided that it is time to dive deep into custom fields for variations as well, at least because recently I’ve been adding one to my Inventory Sync for WooCommerce plugin:

Example of adding a custom field to WooCommerce variations
My plugin adds this product variation field, which allows you to decide whether to exclude this specific variation from the stock sync or not.

In this tutorial, we’ll learn how to work with WooCommerce’s custom fields for variations. Since the new WooCommerce product editor is still in development, I’ll show you examples of how to do it only for the classic product editor, which is widely in use, by the way.

How to Show Variations on the Shop Page

When you visit online store websites, you can notice that sometimes product variations (often colors, but not only) could be displayed nicely on the shop and category pages.

Something like this:

WooCommerce show color variations on shop page

But how can we do it on our store created with WooCommerce as well? The thing is that, by default, WooCommerce doesn’t have a feature that allows you to show variations on shop and category pages.

In this tutorial, I will show you how you can do that. More than that, I am going to show you multiple approaches:

Ok, let’s dive into that.

Auto-Select a Variation Based on the Current Attribute

Ok, I guess, a picture is worth a thousand words, so please take a look at the screenshot below, of what I am going to explain to you in this tutorial.

Auto-select a variation based on the current attribute
It is an example of an attribute archive page in WooCommerce, where variations are displayed (how to display them here), and not only that – the appropriate variation based on the current attribute is automatically selected.

How to Show Variations as Single Products in WooCommerce

In this tutorial, I will show you an example of how you can configure your WooCommerce store to display variations as single products.

If you’re wondering how it may look on your store pages, below is a screenshot for you:

WooCommerce show variations as single products
“Hoodie – Blue, Yes” is a variation.

Get Product Variations Programmatically

Soon, you will learn three ways how you can get product variations programmatically in your code. In the first and in the second way we are simply going to use the WC_Product object (or, to be more specific, the WC_Product_Variation object). The third way is going to be a little bit more interesting – I will show how to do it via the WooCommerce REST API.

Frankly, I remember, I wanted to publish this mini-guide a long time ago, I believe I didn’t because I found an amazing explanation on another website. But guess what – today that website doesn’t exist anymore. So, here we go.

How to Select Multiple Variations in WooCommerce

If you were ever looking for a plugin that allows your WooCommerce store customers to select multiple variations at the same time, I have good news for you – you can easily do it without using any complicated plugins for WooCommerce.

The thing is that WooCommerce already has the feature you need; it is called “Group Products”. The thing is that this feature requires just a little bit of extra configuration. And that’s exactly what we’re going to do in this tutorial.

This is how it is going to look as a result:

WooCommerce select multiple variations
As you can see from the screenshot, we can select multiple variations along with a simple product as a part of a grouped product.

Variation Swatches Without a Plugin – Add a Color Variation

In this tutorial, I will show you how you can create variation swatches programmatically without using any plugins. However, if you change your mind and decide to switch to a plugin, I would highly recommend the one I developed myself.

As an example, we’re going to create a color variation. So, if by default, the variations are displayed like a select dropdown:

default variation switcher on WooCommerce product page
Wouldn’t it be better to add color variations here?

But we will learn how to display them as color swatches. This is our goal for this tutorial:

woocommerce variation swatches without plugin