How to Fix WooCommerce Out of Stock Variations Appearing on the Product Page

This is something that I see from time to time on WooCommerce stores. A product will have variations but include all variations in the dropdown whether or not they’re in stock. This can be caused by themes, other plugins or even, believe it or not, the number of variations you have for the product. The little snippet below will help fix this problem for you.

This problem usually appears when you have a variable product with over 30 variations. On products under 30 WooCommerce will normally either remove or disable sold out variations using javascript. When you have over 30 it changes the way the dropdown works. Instead of being aware of the stock status of each variation, you have to select the option and wait for an ajax call to see if it’s available or not. As a shopper this is really annoying, and you could expect them to leave quickly.

To solve this simply enter the below code into your functions.php file or custom functions plugin. Instead of having the threshold set at 30 it will increase it to 50.

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

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

If you want to restirct it to one product only simply filter the product

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

function wcsuccessacademy_wc_ajax_variation_threshold( $qty, $product ) {

if( $product->get_id() == 765 ) { // 765 is the product ID
return 50;

}
}

And to exclude a product enter it as follows

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

function wcsuccessacademy_wc_ajax_variation_threshold( $qty, $product ) {

if( $product->get_id() !== 765 ) { //765 is the product ID
return $qty;
}

return 50;

}
Written by John Cook
I've been using WordPress for over 6 years and Woocommerce for over 4 years. In that time I have explored the depths of customizations and helped many online retailers grow and expand their business using the Woocommerce selling platform. My aim here is to help enable Woo users around the globe get even more out of their e-commerce platform of choice.