How to create default attributes in WooCommerce
Today, we’ll show you how to create default product attributes in WooCommerce with a plugin, manually from the WooCommerce dashboard, and programmatically via PHP. This is usually overlooked but it can have a huge impact on your variable products’ conversion rates.
What are WooCommerce attributes?
WooCommerce allows you to add information to your products through attributes. You can apply these attributes to any new or existing products. For example, common attributes are sizes, and colors for clothing items, the operating system for mobile phones, and so on. These attributes are global, meaning that instead of setting a size and color attribute for each new product, for example, you create predefined Size and Color attributes and apply them to the different products. In WooCommerce, attributes are very important to:
- Filter Products: Users can filter products based on a specific attribute. For example, medium-size jeans.
- Create variable products: To create variations of a product, first, you need to define attributes for it. Once you have the attributes, you can use them to create the different variations of the product. So, if you sell jeans, for example, this means that you can’t create jeans size variations if you haven’t created the size attribute for them.
Now, let’s better understand why default attributes are so important for every WooCommerce store.
Why create default product attributes?
One of the first things we learned when we started our online store was the importance of creating default product attributes. In the beginning, our conversion rates weren’t very good and a lot of our users would tell us that the Add to Cart button was disabled.
However, every time we checked everything was working well. The worst part is that we were losing a lot of sales because many of those shoppers would never come back to our store. Why would they if they couldn’t buy? It took us some time to realize that the problem was that we hadn’t set default product attributes in WooCommerce.
Product Variations and Default Attributes
As you probably know, there are 4 types of products in WooCommerce: simple, variable, grouped, and external/affiliate. Today, we’ll focus on the variable products and their default attributes. When you create product variations in WooCommerce, you should create the default combination of those attributes. Otherwise, the add to cart button on your product page will appear as disabled until the users select the combination of attributes they want.
The problem is that many of them simply see that the button is blocked, think that the page doesn’t work, and leave. That’s why if you have variable products in your WooCommerce store, creating default product attributes is a must.

How to create default product attributes in WooCommerce?
There are three ways to create default product attributes in WooCommerce:
- With a plugin
- Manually via WooCommerce admin
- Programmatically via PHP
If you’re not a developer, the easiest way is to use a plugin. With the PHP script, on the other hand, you can do the same thing with a bit of coding without installing anything. It’s important to note that you can get the same result with both methods so choose the best one for you depending on your skills.
1) Create WooCommerce default product attributes with a plugin
The easiest way is to use a plugin to set default product attributes. For this guide, we’ll use Direct Checkout for WooCommerce. It’s a freemium plugin that has a free version with basic features and 3 premium plans. This tool has thousands of active installations and it’s one of the best in the market.
1. The first thing you have to do is install and activate the plugin. So, go to your WordPress dashboard > Plugins > Add New. Then, search for Direct Checkout for WooCommerce, click Install Now, and when the installation process finishes, click Activate. Alternatively, you can check out this page and choose either the free version or one of the premium plans.
2. Then, go to the WooCommerce section on the sidebar, and click the Direct Checkout option.
3. Go to the Products section and enable the last option Add default attributes in variable products. Remember to save the changes.
That’s it! You’ve created default product attributes with Direct Checkout for WooCommerce! Now, the first attribute of each product will be selected by default and this will be applied to all your variable products. So, if you want to change the default attributes, you need to modify the order of the attributes and place the attribute you want to set as the default first.
2) Manually via WooCommerce admin
You can also create default product attributes manually via the WooCommerce dashboard. This method is quick and easy but can be time-consuming if you have many products. If that’s your case, we recommend you use the Direct Checkout plugin or the PHP script described in section 3.
-
- To set default attributes manually, first, in the WooCommerce dashboard go to Products and then click on your variable product.
- Then, select Variable product from the Product data dropdown.
- After that, under Variations, you have to select the Default Form Values. Those are the attributes that will be selected by default for that product.
That’s it! You’ve created default product attributes manually! Now when a shopper visits the variable product page, it will show the attributes you selected by default and the Add to Cart button will be enabled.

Keep in mind that you have to set the default attributes for each variable product that you have. That’s why if you have many products, we recommend using the Direct Checkout plugin or the PHP script that we’ll describe below.
3) Create default products programmatically (PHP)
If you don’t want to install any plugin on your site and you have some coding skills, you can create WooCommerce default product attributes programmatically with a bit of PHP.
TIP: Create a child theme
Before you start, we recommend you create a child theme. If you don’t have one, simply install any plugin that you like or code it yourself. For more information about how to create a child theme, check out our step-by-step guide. This is important because if you change the parent theme’s files, the next time you update the theme, you will lose all your customizations. However, if you modify the child theme, your changes won’t be overridden by the theme’s new version.
Now that you’ve installed the child theme, let’s get into it.
PHP Script
To set default variation programmatically in WooCommerce, you’ll need to modify the functions.php file. Simply go to wp-content/yourtheme/functions.php on your child theme. Here, we’ll show you the full code and then we’ll explain its main parts. So the full PHP script to create WooCommerce default product attributes programmatically is the following:
add_action('woocommerce_before_single_product_summary', 'quadlayers_product_default_attributes'); function quadlayers_product_default_attributes() { global $product; if (!count($default_attributes = get_post_meta($product->get_id(), '_default_attributes'))) { $new_defaults = array(); $product_attributes = $product->get_attributes(); if (count($product_attributes)) { foreach ($product_attributes as $key => $attributes) { $values = explode(',', $product->get_attribute($key)); if (isset($values[0]) && !isset($default_attributes[$key])) { $new_defaults[$key] = sanitize_key($values[0]); } } update_post_meta($product->get_id(), '_default_attributes', $new_defaults); } } }
Now, let’s break it down.
The Hook
We use the hook woocommerce_before_single_product_summary. This action hook will run before the page loads so it allows you to add some logic before the user sees it.
add_action('woocommerce_before_single_product_summary', 'quadlayers_update_product_default_attributes');
Check Default Attributes
Then, we check if the meta_default_attributes exist so as not to run the script twice.
if (!count($default_attributes = get_post_meta($product->get_id(), '_default_attributes'))) { $new_defaults = array();
Attributes
After that, we get the whole array of attributes
$product_attributes = $product->get_attributes(); if (count($product_attributes)) {
And then we iterate over that array of attributes.
foreach ($product_attributes as $key => $attributes) { $values = explode(',', $product->get_attribute($key));
We get the first value of each attribute.
if (isset($values[0]) && !isset($default_attributes[$key])) {
If the attribute doesn’t have a value, then the script will add the first value of the available options in $new_defaults.
$new_defaults[$key] = sanitize_key($values[0]); } }
Then, it saves $new_defaults in the meta_default_attributes. This will be checked so as not to run the script twice.
update_post_meta($product->get_id(), '_default_attributes', $new_defaults); } } }
Conclusion
All in all, not setting default attributes for variable products can affect your conversion rates and make you lose many sales. The Add to Cart button will appear as disabled so users may think that they can’t buy and leave.
The good news is that adding default attributes is an easy task. If you don’t have coding skills, you can use Direct Checkout for WooCommerce and add the default product attributes with a couple of clicks. Additionally, if you don’t have many products, you can do it manually via the WooCommerce dashboard. On the other hand, if you prefer to do it programmatically, you can set the default attributes with a bit of coding.
If you want more information about how to improve your WooCommerce site programmatically, you can have a look at our guides about how to add to cart function and to implement AJAX add to cart on your site.
Which of these methods are you going to use? Let us know your thoughts in the comments section below!
15 Comments
Add comment Cancel reply
You must be logged in to post a comment.
Hello,
I’ve tried this code but it doesn’t seem to be working with the latest WP&Woo – does it need updating?
And more importantly, is it possible to amend this snippet so that it only selects the first available option from one only attribute? For example, if you have size and colour attributes, would it be possible to select only first size option (and not select anything from the colour)?
Thanks!
Thanks!
Hello mate
Please check this with the storefrom theme
Some themes could be incompatible with this hooks
Hi,
I just tried to integrate it in my child theme, unfortunately it seems to have a problem with Astra Pro theme too. No standard variant is set to the products.
I’m also looking for a solution how I can set the default size of my clothes to ‘medium’ as pre-selected attribute, if available. Not enough coding background to get it done myself at the moment.
I haven’t dove into the update_post_meta() function too deeply, but I know it performs a database write. If it does this every time, even if the value hasn’t changed, this would cause a DB write on every page load, which is a terrible performance and resource usage oversight. I would find a better way to change this value on the fly, or set it at save/import time.
Hey Zachary. Hi
Of course, few database requests means more performance, but WP and WC are constantly retrieving and updating values into the database.
This is the way a website works
Hi Zachary,
What would be the best attributes to add to a handmade jewelry website?
Thank you.
Hello,
i just add product variations swatches plugin but
the page continues was showing me and the others attributes maybe by default theme.
what can i do to change the default?
Thanks but the PHP code is not working. Please update the code.
Hi Samuel, to help you out, could you please tell us:
1) What error do you get?
2) What theme are you using?
3) Have you tried using Storefront?
Thank for your php code.
It working for me. (Woodmart Theme).
You should clear cache and restart sevice..
Hello! We want our default to stay “no default product variation” but every few weeks, all of our variable products end up with default variations (I believe it’s the first in each attribute list, but I didn’t check every single product to confirm that). Because of the nature of the products we sell, it’s causing issues — customers keep buying codes for the wrong product because they don’t bother to change away from the default — we want to force them to make a selection. Any insight? We cannot find any pattern to why this is occurring. (note we use method 2 from above, Manually via WooCommerce admin)
Thank you so very much! I was desperate for this information, I just hadn’t seen the possibility to set defaults in the Variable product field. I’m working towards opening my shop and thought it can’t be good not to have a premade setting. Very glad I found your site.
Thanks Sonja, we’re glad it was useful
@Brenda, couldn’t you add another variation that costs a bit more, call it “manual configuration”, and set that as default? Then you can get back to those buyers about the configuration, and you can have your expenses covered by adding to the price.
The manual code doesn’t work anymore. Any suggestions?