How to change the ‘Default Variant’ in WooCommerce

As a standard, when you create a new WooCommerce Variable Product there is no default variant set. This can be very handy for a simple store that just wants to quickly get products out to sell. A problem arises however if you have additional data assigned to the product, for example personalisation.

This issue can be easily solved by assigning a default selection on the Product Edit page.

default-option-screenshot

This is fine and works perfectly, however what if you have a large shop, or have imported a shop containing multiple Variable Products? It can be a pain and rather time consuming to go back and set a default for each Variable Product.

You can either overwrite this within your child theme or, my preferred method, within a plugin. I will be showing you how I created my plugin which allows you to remove the standard ‘Select an option’ from WooCommerce variable products and force your own default variant based on ID, or Price, both Low->High and vice versa across the whole site. If you would rather just use my plugin, you can find it here.

First off, let’s start by creating the plugin folder. Below is a picture of the file structure you will need.

Now you have the file structure you will need to open the core Plugin file. which in this example is woo-force-default-variant.php. As with all plugins you need to start with the necessities.

The next step is to ensure that all the required files are being included. For the PHP files we just need to include() them, this is done and explained below.

Congratulations, you have created the core file for your new plugin.

The next file we will be creating is settings.php. Within this file we will be creating our own section within the WooCommerce settings menu. Below is the code I have used to add an additional section into the Products menu. If you would like more explanation about this then please read more here.

Not that you can set how you want your variants to be ordered, the next step will be to update the variant drop down. There are a couple of ways to do this. You could overwrite the WooCommerce variable.php template file. The main drawback to this is that it would stop that specific file from being updated with newer versions of WooCommerce and updates are important for security and just a generally well run site. Thankfully all the code we will need to overwrite is wrapped up neatly within a filter. This filter is woocommerce_dropdown_variation_attribute_options_args and it is easily overwritten.

Below are the contents of the last PHP file we have and that is variations.php. I have commented on my code, explaining what each part is for. As you can see we are not rebuilding anything WooCommerce has done, we are simply using the existing Variant information and changing the default option.

If you do not wish to reorder by Price, then the above file will simply ignore the price option and carry on with the WooCommerce default, which is ID.

You now have a functioning plugin that will allow you to force a default Variant across the board, either by ID, or defaulting to the Lowest or Highest Price. There is one last thing we need to do. WooCommerce as standard comes with a ‘Clear Selection’ option. As we are always going to be forcing a default variant we no longer need this option, so last but not least we need to add the below into the clear-removal.php.

Once this has been done, you are completely finished. If you have not already done so add this plugin to your WordPress installation your-site.com -> wp-content -> plugins and navigate to your-site.com/wp-admin. Jump to the Plugins page and activate your fresh new plugin. You are now a plugin developer, well done!
Once the plugin is activate navigate to WooCommerce -> Settings -> Products -> Variants and choose how you want your variants to be sorted.

Good luck!

1 thought on “How to change the ‘Default Variant’ in WooCommerce”

  1. I was looking for a simple solution and this one worked really well for me. I’d just like to point out that there’s a little bug – when you were coding the variations.php file, in the end you deleted the “Choose an option” variation but you forgot to reduce the number of possible variations. Thus the last option/attribute on a product page was not-selectable.

    A quick solution is to remove the line $args[‘selected’] = $prices[0][1]; from the variations.php file.

Leave a comment