Categories
WooCommerce

Increase the default number limit of variations for a product in WooCommerce

In WooCommerce, you can add as many variations as you want! However, if you have over 30 of them, WooCommerce will make an AJAX call (an extra request to the backend) to fetch the details for each variation.

Why is the limit even there?

While it can be annoying, I think it’s a good idea to have a general limit. The general idea is that if you have too many variations, it’s going to make it really difficult for a customer to choose what to buy. If you have over 30 variations, you might want to think of splitting it up into more products so it’s not so confusing for people.

However, in some use cases, 30 variations is not a lot! For example, shoes and clothing items can easily have more than 30 variations and still be totally acceptable. That’s because it’s less of a choice for the customer and more of figuring out what size they need.

Lets change the default limit

To change the limit, we’re going to use a filter!

WooCommerce: 4.5.2
Filter: woocommerce_ajax_variation_threshold
Source: https://github.com/woocommerce/woocommerce/blob/4.5.2/includes/wc-template-functions.php#L1660

The code:

<?php // leave this line out unless you're starting a new PHP file

add_filter( 'woocommerce_ajax_variation_threshold', function( $quantity, $product ) {
	return 50;
 }, 10, 2 );

In that code, we’re just returning a new quantity of 50 for the maximum value allowed for variations.

If you wanted to do this for just a single product, here’s what that would look like:

<?php // leave this line out unless you're starting a new PHP file

add_filter( 'woocommerce_ajax_variation_threshold', function( $quantity, $product ) {
	// If you want change the default limit for just one product, set the ID here:
	if ( 401 === $product->get_id() ) {
		return 100;
	}
 }, 10, 2 );

If you have any questions or suggestions for improvements, let me know!