By default, customers are able to select any variation from the drop down of a variable product. You might want to prevent them from choosing an out of stock variation though. A good way to do this would be to disable the option. This way, it will still be shown in the dropdown but will be greyed out and customers won’t be able to select it.
To do this, all you need is the following snippet:
add_filter( 'woocommerce_variation_is_active', 'snippetpress_disable_out_of_stock_variations', 10, 2 ); function snippetpress_disable_out_of_stock_variations( $is_active, $variation ) { if ( ! $variation->is_in_stock() ){ $is_active = false; } return $is_active; }
As you can see in the screenshot below, before adding the snippet, customers could select the out of stock “small” variation. When the snippet is in use, the option still shows, but cannot be selected.

Where to add the snippet?
The snippet should be placed at the bottom of the functions.php file of your child theme. Make sure you know what you’re doing when editing this file. Alternatively, you can use a plugin such as Code Snippets to add the custom code to your WordPress site. If you need further guidance on how to add the code, check out our post on How to Add WordPress Snippets.
Leave a Reply