Tips, WooCommerce Tutorials

Modify the Ajax Variation Threshold

When your variable product has more than 30 variations, WooCommerce starts to use ajax to load your selected variation. This changes the way the dropdown fields work – where before you could select some options and others would become unavailable/disabled, now you have to select all options before finding out the variation you selected is not available. You may have noticed this when using our WooCommerce Attribute Swatches plugin to change your product dropdowns into graphical swatches.

Fortunately, as with most things WooCommerce, you can modify this setting. Try adding this to your theme’s functions.php file:

function iconic_wc_ajax_variation_threshold( $qty, $product ) {
    return 50;
}

add_filter( 'woocommerce_ajax_variation_threshold', 'iconic_wc_ajax_variation_threshold', 10, 2 );

This means ajax will only kick in when there is more than 50 variations. You can adjust this accordingly.

You could also use the $product object to do this for a specific product only. Very useful!

Avatar

James is the founder of Iconic and an experienced WooCommerce plugin developer. He has been featured on numerous WordPress publications.

He is a keen photographer and currently shoots using his favourite mirrorless camera, a Fuji X-T20.

25 Responses

Avatar

Hi,
That looks like just the answer to my problem, thank you! But where can I find the functions.php file please? Is it in a plug in or is it in the woocommerce original variations setting and how do I access it?

Jhim
Jhim admin 8 months ago

Hi Aude,

Fantastic! you can find the functions.php in your currently active theme directory which you can access via FTP or Cpanel. Aside from the functions.php, you can also add this using 3rd party plugin called Code Snippet. You can search it in WordPress plugin repository. I hope this helps :)

Avatar

This seems to cause some issues now...is there any update? Thanks!

Jhim

Hi Jessie,

Are you using the latest version of our plugin? I tried to replicate the issue on my end but this still works on my end. Can you please let me know what is the error you've encountered?

Avatar
Lacy French 10 months ago

Hi, I am also having issues. When I first built my client's site the code snippet worked, now when it is added it causes their product to not show up at all once it is clicked on from the shop page.

Avatar
Gianluca Borg 1 year ago

I have a product that has over 1000 variations and when return is set 1000 it is not showing up. Is there a limit for this?

Avatar
Joel Inman 1 year ago

Hey Gianluca,

you need to set it to more than your maximum variations.

So try:

function custom_wc_ajax_variation_threshold( $qty, $product ) {
return 1100;
}

add_filter( 'woocommerce_ajax_variation_threshold', 'custom_wc_ajax_variation_threshold', 10, 2 );

Avatar

Thanks for code :)
The value that I will enter for example "return 50;" can be increased without limits? Or better not to exceed a certain threshold to avoid slowdowns? Unfortunately, on my site I will have to use many combinations of variations, sometimes even more than 120.

Another question, after entering the code in the function.php, how should I set the settings in the backend? For example, by default the value in "Ajax variation threshold" is set to "30" while the item "Disable ajax threshold" is not flagged. How should I set these values ​​once I have entered your code in function.php?

Thanks!

Avatar

Thanks for your info that helped me a lot and solved my problems. Now we have no more problems with variations. Thanks a lot

Jhim
Jhim admin 2 years ago

Hi Mario,

On behalf of our awesome and talented Lead Developer, you're welcome and glad that it helps <3

Avatar
antonella 2 years ago

Hi, I used your code and it works perfectly! Only on some products it does not seem active ... they have 225 variants and I changed the return 200 code to return 230, but it still doesn't work ... where can the problem be?

Jhim
Jhim admin 2 years ago

Hi Antonella,

You might try to deactivate other plugins to check whether this was caused by a conflict with other plugins or themes which is common in WordPress if something is not working as expected.

Avatar

Do you think it'll have any performance benefits for small sites with a few variations?

Avatar

hi, what is the function of the digits 10 and 2 at the end of the string?
many thanks

Avatar
Websites Templates 4 years ago

Thanks for this. I really great.. I was working last couple of hours but not find the issue then I found this and it's save my time thanks...

Avatar

Thanks for this, 1 follow up question. How do I only apply this to certain products, like you mentioned is possible? Thanks

Avatar
James Kemp admin 4 years ago

Hey, sorry - didn't see the comment come in! Inside the function you can use the $product variable to check the ID:

if( $product->get_id() !== 42 ) {
return $qty;
}

return 50;

This would return the normal qty for all products where the ID *is not* equal to 42, otherwise, return 50.

Better yet, use the product ID to fetch a meta field, then you can tick/untick this in the backend of WordPress on a per-product basis.

Avatar
Arnim Pommeranz 2 years ago

Hi James, thank you for this great solution, that save me really. Unfortunately, it also has a disadvantage. It takes much longer to put the product in the shopping cart. Could you please explain your suggestion "Better yet, use the product ID to fetch a meta field, then you can tick / untick this in the backend of WordPress on a per-product basis."? Best with code example.

Many Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *