As an online store owner, offering diverse product options is essential to cater to different customer preferences and maximize sales. Product variations allow you to provide multiple options for a single product, such as different colors, sizes, or materials.
Although WooCommerce offers adding product variations by default but to stand out in the competitive world of eCommerce truly, you need to go beyond the basics and customize your WooCommerce product variations for a more personalized and engaging shopping experience.
In this blog post, we will explore how to customize WooCommerce product variations programmatically and also through plugins, empowering you to create an attractive, user-friendly, and efficient online store.
So let’s begin.
- What is Product Variation
- Why Should You Customize WooCommerce Product Variations
- How to Create Woocommerce Variable Products With Attributes Through Coding
- WooCommerce Variable Products and Their Outputs
- How to Create Woocommerce Variable Products With Attributes Using Plugin
- Recommended Plugins for Customizing Product Variations
- Best Practices for Customizing WooCommerce Product Variations
- Troubleshooting Common Issues With Product Variations
From Launching to Customizing Your Woocommerce Stores, Cloudways Is at Your Service
Whether you’re a beginner or an expert, Cloudways Platform is based on UI, where you can create and customize your online store in a few seconds.
What Is Product Variation in WooCommerce?
In WooCommerce, a product variation refers to a specific version of a product that has distinct attributes like size, color, material, or style. These variations allow you to offer multiple options for a single product within one listing, making it easier for customers to choose the exact configuration they want without navigating to a different product page.
Each variation can have its own price, SKU, stock status, image, and shipping options, giving store owners flexibility in handling multiple product configurations. This also improves SEO by reducing duplicate listings and helps customers see all available options in one place.
By default, WooCommerce includes basic support for creating variations. However, to make your store more user-friendly and visually appealing, customization is key; either through plugins or programmatically. This includes things like swatch selectors, dynamic price updates, image changes on attribute selection, and more refined filtering.
Why Should You Customize WooCommerce Product Variations?
Customizing WooCommerce product variations can significantly improve the shopping experience for your customers and potentially increase sales.
Here are some key benefits of customizing product variations in your WooCommerce store:
- Allows you to create a more visually appealing and user-friendly interface.
- Helps you cater to individual customers’ unique preferences and requirements.
- Lets you efficiently manage your inventory by combining similar products into a single listing with multiple options.
- Enables the better organization of products on your website.
- It can help you upsell and cross-sell related products by highlighting complementary items or more premium versions of the same product.
- It can improve your search engine optimization (SEO).
- It simplifies the product management process, making it easier to add, edit, and remove product options as needed.
- Allows you to adapt to changing market trends, customer preferences, and new product releases, ensuring your store remains competitive and relevant.
Ready to take your WooCommerce product variations to the next level?
Experience unparalleled performance, security, and customization options with Cloudways WooCommerce Hosting.
How to Create Woocommerce Variable Products With Attributes Through Coding
In this section, we’ll walk through how to create variable products in WooCommerce programmatically. This can be particularly useful if you’re managing a large catalog and want to automate the process of adding variations like size, color, or material.
The function provided defines a variable product ID and creates variations under that parent product. It sets up key attributes such as SKU, price, stock quantity, and custom product attributes using a structured, multidimensional array.
Before the variation is created, the function checks if the attribute and term already exist. If not, it registers the attribute, adds the term, and assigns it to the parent product. This ensures the product variation is correctly linked to its parent and includes all the necessary data to appear on the front end and within your store’s admin dashboard.
function create_product_variation( $product_id, $variation_data ){
$product = wc_get_product($product_id);
$variation_post = array(
'post_title' => $product->get_title(),
'post_name' => 'product-'.$product_id.'-variation',
'post_status' => 'publish',
'post_parent' => $product_id,
'post_type' => 'product_variation',
'guid' => $product->get_permalink()
);
$variation_id = wp_insert_post( $variation_post );
$variation = new WC_Product_Variation( $variation_id );
foreach ($variation_data['attributes'] as $attribute => $term_name )
{
$taxonomy = 'pa_'.$attribute;
if( ! taxonomy_exists( $taxonomy ) ){
register_taxonomy(
$taxonomy,
'product_variation',
array(
'hierarchical' => false,
'label' => ucfirst( $attribute ),
'query_var' => true,
'rewrite' => array( 'slug' => sanitize_title($attribute) ),
)
);
}
if( ! term_exists( $term_name, $taxonomy ) )
wp_insert_term( $term_name, $taxonomy );
$term_slug = get_term_by('name', $term_name, $taxonomy )->slug;
$post_term_names = wp_get_post_terms( $product_id, $taxonomy, array('fields' => 'names') );
if( ! in_array( $term_name, $post_term_names ) )
wp_set_post_terms( $product_id, $term_name, $taxonomy, true );
update_post_meta( $variation_id, 'attribute_'.$taxonomy, $term_slug );
}
if( ! empty( $variation_data['sku'] ) )
$variation->set_sku( $variation_data['sku'] );
if( empty( $variation_data['sale_price'] ) ){
$variation->set_price( $variation_data['regular_price'] );
} else {
$variation->set_price( $variation_data['sale_price'] );
$variation->set_sale_price( $variation_data['sale_price'] );
}
$variation->set_regular_price( $variation_data['regular_price'] );
if( ! empty($variation_data['stock_qty']) ){
$variation->set_stock_quantity( $variation_data['stock_qty'] );
$variation->set_manage_stock(true);
$variation->set_stock_status('');
} else {
$variation->set_manage_stock(false);
}
$variation->set_weight('');
$variation->save();
}
The function handles everything from registering custom taxonomies to updating variation meta details like pricing, inventory settings, and attribute mappings.
This approach saves time and ensures consistent setup across multiple variable products without needing to manually input everything through the dashboard.
WooCommerce Variable Products and Their Outputs
WooCommerce variable products let you create a single listing with multiple variations, such as colors, sizes, or materials. Each variation has unique attributes, stock levels, and pricing options, providing a personalized shopping experience for customers.
To work with variable products in WooCommerce programmatically, use these key code snippets and functions added to the functions.php file:
- wc_get_product($product_id): This function retrieves the product object for a given product ID. It’s used to access the product’s data, such as its attributes, prices, and stock levels.
- wp_insert_post( $variation_post ): This WordPress function is used to create a new post or update an existing one. In the context of WooCommerce, it can be used to create a new product variation.
- WC_Product_Variation( $variation_id ): This function creates an instance of the WC_Product_Variation class, which represents a specific product variation. You can use this object to get or set properties of a product variation, such as its attributes, prices, and stock levels.
- register_taxonomy: This WordPress function is used to create a new taxonomy, which is a way of organizing and categorizing content on your website. In WooCommerce, product attributes are stored as custom taxonomies.
- wp_insert_term: This WordPress function is used to create a new term within a taxonomy. In WooCommerce, product attribute values are stored as terms. This function is used in the IF condition to check if the term name exists and, if not, create it.
- $post_term_names: This variable stores an array of term names associated with the parent variable product. It’s used to check if a specific term is already set for the product.
- wp_set_post_terms: This WordPress function is used to set or update the terms associated with a post. In WooCommerce, it can be used to associate attribute values (terms) with a product or product variation.
- update_post_meta: This WordPress function is used to update the metadata associated with a post. In WooCommerce, it’s used to save the attribute data for a product variation.
- $variation->set_sku: This method of the WC_Product_Variation class is used to set the SKU (Stock Keeping Unit) for a product variation. The SKU is a unique identifier for each product variation, which helps with inventory management.
- $variation_data[‘sale_price’]: This array key contains the sale price value for a product variation. It can be used to set or update the sale price of a specific product variation.
- $variation_data[‘stock_qty’]: This array key contains the stock quantity value for a product variation. It can be used to set or update the stock level of a specific product variation.
Future-Proof Your Online Store with Autonomous
Keep your online store ready for all the unexpected traffic spikes with Cloudways Autonomous. Stay ahead of the curve with advanced scalability and cutting-edge technology.
How to Create Woocommerce Variable Products With Attributes Using Plugin
In WooCommerce, you can customize product variations using plugins that provide additional options and functionalities.
There are several plugins available to help you customize product variations in WooCommerce. Some popular choices include:
- Variation Swatches for WooCommerce
- WooCommerce Additional Variation Images
- YITH WooCommerce Color and Label Variations
For the purpose of this blog, we will be using Variation Swatches for the WooCommerce plugin.
Here’s a step-by-step guide on how to create WooCommerce variable products with attributes using the Variation Swatches for WooCommerce plugin:
Step 1: Install and Activate the Plugin
- Go to your WordPress dashboard
- Navigate to ‘Plugins’ > ‘Add New’
- Search for ‘Variation Swatches for WooCommerce’

- Click ‘Install Now’, and then activate the plugin
Step 2: Configure the Plugin Settings
- Go to ‘Settings’

- Configure the settings according to your needs. For example, you can adjust the swatch style, size, and shape
Step 3: Create or Edit a Variable Product
- Go to ‘Products’ > ‘Add New’ to create a new product or click ‘Edit’ below an existing product
- In the ‘Product data’ section, choose ‘Variable product’ from the dropdown menu

Step 4: Add Attributes
- In the ‘Attributes’ tab, add the attributes you want to use for variations (e.g., color, size)
- Click ‘Add,’ fill in the attribute details, and check the ‘Used for variations’ box

- Click Save attributes
Step 5: Generate Variations
- Go to the ‘Variations’ tab, and click ‘Create variations from all attributes’ to automatically generate all possible variations

- Configure each variation’s details, including price, stock, and image

Step 6: Save your Changes
- After configuring your product variations, click ‘Update’ or ‘Publish’ to save your changes

- Now you can see the variations you just created.
Recommended Plugins for Customizing Product Variations
There are several plugins available that can help you customize product variations on your eCommerce website. Here’s a list of recommended plugins, mainly for WordPress and WooCommerce:
- Variation Swatches for WooCommerce
- Smart Swatches
- YITH WooCommerce Color and Label Variations
- XT Variation Swatches
- WPB Product Slider for WooCommerce
- Swatchly
- Improved Variable Product Attributes for WooCommerce
- WooCommerce Product Variations Matrix
Best Practices for Customizing WooCommerce Product Variations
Customizing WooCommerce product variations can significantly improve the user experience and increase sales on your online store. To get the most out of customizing product variations, follow these best practices:
- Plan and organize attributes and variations for clarity.
- Use high-quality images for each variation and optimize them for web performance.
- Maintain consistent naming across all product options.
- Set accurate prices and stock levels for each variation.
- Test regularly to ensure everything works smoothly.
- Use reliable plugins to extend variation features if needed.
Troubleshooting Common Issues With Product Variations
WooCommerce product variations can sometimes encounter issues that may affect your online store’s functionality or user experience. Here are some common issues with WooCommerce product variations and their respective solutions:
Problem#1: Variations Not Showing or Appearing Correctly
Sometimes you set the variations, but they don’t appear correctly. Reasons for this can vary, but you might consider doing the following to fix it:
Solution:
- Ensure that you have set up product attributes and variations correctly. Attributes should be added in the “Attributes” tab, and then variations should be created in the “Variations” tab under each product.

- Check if there are any theme or plugin conflicts by deactivating other plugins one by one and switching to a default WordPress theme temporarily. If the issue is resolved, identify the conflicting plugin or theme and find a suitable alternative.
Problem#2: Ajax Add-To-Cart Not Working for Variable Products
Sometimes the add-to-cart doesn’t work for variable products.
Solution:
You might fix it by doing the following:
- Ensure that your theme is compatible with WooCommerce and supports AJAX add-to-cart functionality for variable products. Or add the AJAX add-to-cart plugin.

- Check for plugin conflicts that may be causing the issue.
Problem#3: Changes to Variations Not Saving
Sometimes you are not able to save the new variations.
Solution:
Here’s how you might fix it:
- Make sure your web hosting environment meets WooCommerce’s minimum requirements, including the PHP version and memory limit. I recommend using Cloudways because it uses the latest PHP version and is scalable, making it an excellent choice for hosting WooCommerce stores efficiently.
- Increase the server’s max_input_vars value in your php.ini file, as low values may cause issues when saving a large number of variations.
Problem#4: Performance Issues With a Large Number of Variations
Sometimes you face performance issues after setting the variation for your products.
Solution:
Here’s how you might improve the performance of your store;
- Use a caching plugin, such as WP Rocket or W3 Total Cache, to improve your site’s performance.
- Optimize your database regularly to keep it clean and efficient.
- Consider using a plugin that specializes in handling large numbers of variations, such as WooCommerce Product Variations Matrix or Variation Swatches for WooCommerce.
Summary
In conclusion, creating customize WooCommerce product variations can be a powerful way for developers to enhance their client’s e-commerce websites.
This blog post has provided a code snippet example for creating product variations, as well as explored the option of using plugins to add custom fields for product variations.
By leveraging the flexibility of variable products, developers can create more engaging and customizable shopping experiences for their clients.
As always, remember to place your code in the appropriate file, such as functions.php, and continue to experiment with the many possibilities available through WooCommerce.
Q. How do I add custom fields to product variations?
You can add custom fields to product variations in WooCommerce by using plugins like “WooCommerce Custom Fields” or by coding custom solutions. Alternatively, utilize the built-in features of WooCommerce or themes that support customizing product variations.
Q. How do I get product variations in WooCommerce?
Follow the steps below to get product variations in WooCommerce:
- Access your WordPress dashboard and go to WooCommerce.
- Navigate to the product you’re interested in managing variations for.
- Click on the product to open its edit page.
- Look for the “Variations” tab and select it.
- View existing variations and manage them.
- To add new variations, click on the relevant option and follow the prompts.
Q. How do I get all variations of a product in WooCommerce?
A) To retrieve all variations of a product:
- Use the WooCommerce admin panel under the product’s “Variations” tab.
- For developers, use the WC_Product_Variable class and the get_available_variations() method to fetch variations programmatically.
Q. How to set variable products in WooCommerce?
A) Create or edit a product in WooCommerce.
- Select “Variable Product” from the product type dropdown.
- Add attributes under the Attributes tab and enable them for variations.
- Configure individual variations in the Variations tab, adding prices, stock, and unique settings for each option.
Q. What is the difference between simple and variable products in WooCommerce?
A) A simple Product is a single item that has no variations. Whereas, variable product is a product with multiple variations based on attributes such as clothes, shoes, cosmetics, that has different prices, stock levels, and images for each variation.
Q. What are the variation options in WooCommerce?
Variation options in WooCommerce include attributes like size, color, and material, which allow you to offer different versions of a product. These variations can have unique prices, stock levels, and images, providing customers with choices when making a purchase.
Q. What is the difference between attributes and variations in WooCommerce?
Attributes in WooCommerce define the characteristics of a product, such as size or color, while variations are specific instances of a product with different attribute combinations, like a small blue shirt versus a large red shirt. Attributes are used to create variations, allowing customers to choose different options when purchasing a product.
Owais Khan
Owais works as a Marketing Manager at Cloudways (managed hosting platform) where he focuses on growth, demand generation, and strategic partnerships. With more than a decade of experience in digital marketing and B2B, Owais prefers to build systems that help teams achieve their full potential.