WooCommerce: Disable Out of Stock Variations @ Variable Product Dropdown

A nice way to avoid user frustration is to never let them pick a product / variation that is out of stock, only to realize later they can’t purchase it.

A variable product comes with a “select dropdown” on the single product page, from which customers can pick their favorite variation. Problem is that ONLY after selecting this they will find out about price, stock status and may be able to add to cart.

Today, we’ll completely disable (grey-out) those select dropdown options (variations) that are out of stock, so that users don’t waste time and only pick one of those that are in stock. Enjoy!

In this example, thanks to my snippet, the “Medium” variation is greyed out and not selectable on the WooCommerce Single Product page (“Medium” is out of stock)

PHP Snippet: Grey-out Out of Stock Variations @ WooCommerce Single Product Page

/**
 * @snippet       Disable out of stock variations @ WooCommerce Single
 * @how-to        Watch tutorial @ https://businessbloomer.com/?p=19055
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_filter( 'woocommerce_variation_is_active', 'bbloomer_grey_out_variations_out_of_stock', 10, 2 );

function bbloomer_grey_out_variations_out_of_stock( $is_active, $variation ) {
    if ( ! $variation->is_in_stock() ) return false;
    return $is_active;
}

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7+.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce Customization?

Check out these free video tutorials. You can start learning how to customize WooCommerce without unnecessary plugins. Watch me code and learn by example!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

12 thoughts on “WooCommerce: Disable Out of Stock Variations @ Variable Product Dropdown

  1. Hey, I have tried to implement this code, but I am having some issues, would you be willing to take a look and see what might be the problem?

    note: the variable “test” is out of stock.

    1. Hello Seth, thanks for your comment! I just tested this again with Storefront theme and it works perfectly. Maybe your theme (or another plugin) is messing/conflicting with my snippet?

      To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentyseventeen” theme (load the snippet there in functions.php) – does it work? If yes, you have a problem with your current theme or one of the plugins.

      Hope this helps!

      R

      1. Thank you, I turned everything off, I even tried the Storefront theme. It still didn’t seem to work. My dropdown box looks different from yours as well… https://www.dropbox.com/s/uaixvgrjcirbsg2/Screen%20Shot%202019-10-22%20at%204.21.08%20PM.png?dl=0

        I wonder what I’ve done wrong.

        1. Not sure ๐Ÿ˜

  2. Great addition to my websites. Work great! Thank you

    1. Awesome!

      1. I do have a wholesale quick order form on my website that replicates the WooCommerce order form cart. The code snippet doesn’t seem to get applied on this.

        Here’s a link to the quick order form where you can see the variation form used and the various are not greyed out:
        https://silviafindings.staging.wpengine.com/wholesale-quick-order-form

        Here’s a link to a product to test with (SKUs: BC004 BC005 & BC006 are out of stock)
        https://silviafindings.staging.wpengine.com/product/oval-multi-purpose-jeweller-setting-pendant-oval-bezel-mounting-sterling-silver

        How would I apply the same rule in this case?

        Thanks
        Lyse

        1. Hello Lyse, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

          1. Thank you for the offer, but I don’t think I need it that bad.

            1. No problem!

  3. Hei, that’s a really nice tweak..
    Thanks.. I wanna implement it soon on one of my client website.
    ๐Ÿ™‚

    1. Great!

Questions? Feedback? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between: [php]code_here[/php]. Failure to complying with this (as well as going off topic) will result in comment deletion. You should expect a reply in about a week - this is a popular blog but I need to get paid work done first. Please consider joining #BloomerArmada to ask me 1-to-1 WooCommerce questions. Thank you :)

Your email address will not be published. Required fields are marked *