Modify the Ajax Variation Threshold – WooCommerce

Modify the Ajax Variation Threshold – WooCommerce

Fix for too many Variations

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 the WooCommerce Attribute Swatches plugin.

This will also apply if you have a full fledged store where you will need the cart fragmentation feature to dynamically update the cart items without refreshing the page.

If a variable product has more than 20 variations, the data will be loaded by ajax rather than handled inline. It is possible to change this quantity using the woocommerce_ajax_variation_threshold.

Adding the following snippet of PHP code to the functions.php file in the child theme solved the problem:

function iconic_wc_ajax_variation_threshold( $qty, $product ) {
    return 300;
}
 
add_filter( 'woocommerce_ajax_variation_threshold', 'iconic_wc_ajax_variation_threshold', 10, 2 );

This snippet tells WooCommerce to allow 300 variations for a product before it stops embedding the JavaScript that allows the web browser to dynamically enable and disable the different dropdown lists. You may need to adjust it accordingly to your needs. Just make sure it’s higher than the maximum number of variations any single product might have.

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