How to Create Variable Products with Conditional Logic
In this step-by-step guide, you’ll learn how to create WooCommerce variable products with conditional logic.
This feature is not supported out of the box, and most of the solutions you can find out there will force you to work with another entity, so-called “product add-ons” or “product options”, which are, in simple words, just custom fields for order items in most cases. I don’t say it is a bad thing, but sometimes they are unnecessarily overcomplicating things.
The good news is that I developed a solution for you! As usual, it was first requested by a client of mine, but now it has become a fully functional Conditional Attributes for WooCommerce plugin. So, below I will share with you two ways of creating conditional logic for product variations – using my plugin, of course, and a programmatic approach.
This is also an example of a variable product with conditional variations, which we will create:

Creating WooCommerce Conditional Variations Using a Plugin
For this chapter, please install and activate Conditional Attributes for WooCommerce.
The whole process of creating conditional variations here consists of two simple steps.
- First, we will create conditional logic in a product attribute’s settings.
- Second, we will create a variable product as we normally do.
Let’s jump into it, assuming that the plugin is already installed and activated for your WooCommerce store.
Step 1. Setting up conditional logic for product attributes
As you may have noticed, I don’t say it the way that we’re creating conditional logic for WooCommerce variations here, because each variation is represented by multiple product attributes, right? So, depending on these attributes, we will create conditions. In other words, if we create a condition for a specific product attribute, it may cover multiple variations connected to that attribute.
Let’s come back to the example I showed you at the beginning of this article:

Our goal right now is to completely hide the “Spell type” attribute if the selected value of the “Magic level” attribute is either “None” or “Basic”, right?
To do so, in the WooCommerce admin dashboard, navigate to Products > Attributes, then click the “Edit” link near an attribute that is supposed to be a conditional one – the one we are going to hide or show depending on the value of multiple other attributes. Now, let’s click the “Is conditional attribute?” checkbox and create the conditional logic we need:

This is pretty much it with the first step; now let’s proceed to creating conditional variations.
Step 2. Creating WooCommerce conditional variations for a product
Once the conditional logic for attributes is configured, we can create conditional variations when editing a product. The process is quite similar to how we normally create variations, except for the following moments:
- WooCommerce now knows which variation attributes to display depending on selected values of other attributes.
- It is better to add each variation manually to fully understand the process.
In the screenshot below, I am showing you what I mean – I am manually adding a new conditional variation:

That’s pretty much it, guys. Your WooCommerce store’s conditional variations are fully set up and ready to go.
Developing Conditional Logic for WooCommerce Variable Products Programmatically
At this point, guys, you will understand clearly why there are so many “product add-ons” and “product options” plugins over there but not a single one for “conditional variations” themselves.
The whole thing is that WooCommerce not only doesn’t allow you to use conditional logic for product variations, but it is also very strict about validation.
For example, if you try to show or hide a specific attribute selection the following way, it doesn’t work out, because WooCommerce will try to validate the value of the hidden attribute in any case, and no filter hook exists to adjust this validation:
$( 'select[name="attribute_pa_magic_level"]' ).change( function() {
const magicLevel = $(this).val()
const $spellTypeAttr = $( 'select[name="attribute_pa_spell_type"]' )
if( 'none' === magicLevel || 'basic' === magicLevel ) {
$spellTypeAttr.val( '' ).closest( 'tr' ).hide()
} else {
$spellTypeAttr.closest( 'tr' ).show()
}
} );The code itself seems pretty decent (except it uses jQuery, but since WooCommerce uses it too, we can allow that for now), and it works:

But everything will fail at the moment of adding this product into the cart.
So, what are our options here:
- To give up on conditional variations altogether, and instead to use order items custom fields (this is the way many plugin developers will prefer).
- To trick WooCommerce by using a temporary value of a hidden attribute, but then remove it at the moment of creating an order.
For example, we can start by setting up the first available value for our conditional attribute, at the moment we hide it:
const firstValue = $(this).find( 'option[value!=""]' ).first().val()
if( 'none' === magicLevel || 'basic' === magicLevel ) {
$spellTypeAttr.val( firstValue ).closest( 'tr' ).hide()Of course, it is just the beginning, because after that our goal is to make sure that this temporarily selected attribute value is not displayed:
- in the customer’s cart,
- during the checkout,
- in an email to the customer,
- and of course, at the final stage, it is correctly removed from order items.
Of course, all of these scenarios had already been taken care of in the plugin approach, but right now, I will show just an example for the customer’s cart:
add_filter( 'woocommerce_get_item_data', 'rudr_conditional_attribute', 99, 2 );
function rudr_conditional_attribute( $item_data, $cart_item ) {
if(
isset( $cart_item[ 'variation' ][ 'attribute_pa_magic_level' ] )
&& in_array( $cart_item[ 'variation' ][ 'attribute_pa_magic_level' ], array( 'none', 'basic' ) )
) {
// conditions are met
foreach( $item_data as $i => $attribute ) {
if( wc_attribute_label( 'pa_spell_type' ) === $attribute[ 'key' ] ) {
unset( $item_data[ $i ] );
}
}
}
return $item_data;
}So, I hope you get the idea. If you have any questions about this code or about the plugin, feel free to ask in the comments section below.
Misha Rudrastyh
Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me