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;
}
I have been working with WordPress and WooCommerce since 2012 and have developed a deep knowledge of the content management system. Since 2012, I have developed several plugins and designed dozens of websites utilising different frameworks, CMS’s and programming languages.
On a professional level, I’m currently undertaking PhD studies investigating IoT cybersecurity. I recently graduated with First Class Honours and Masters of Information Technology, receiving the Executive Dean’s Award for studies undertaken in the 2021 and 2022 academic years. I have worked in the information technology industry for the past 11 years primarily as a software/web developer specific to design, optimisation, network management and security. My research interests are in the areas of Internet of Things (IoT), 5G and Beyond Networks and information security for wireless networks and software development.
Google Scholar ID: https://scholar.google.com.au/citations?user=0KvpwNEAAAAJ&hl=en
ORCID: https://orcid.org/0000-0001-8113-1296