• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
SnippetPress

SnippetPress

The Home of WordPress Snippets

Vultr Web Hosting
  • Cheat Sheets
  • Reviews
  • Snippets
  • Tutorials

WooCommerce – Disable Out of Stock Variations

June 29, 2020 by SnippetPress Leave a Comment

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.

Filed Under: Snippets, Snippets, WooCommerce

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

E-mail Newsletter

More to See

WooCommerce – Prevent Product Being Added To Cart If A Specific Product Is Already In Cart

July 15, 2020 By SnippetPress

WooCommerce – Display an “Add $X To Get Free Shipping” Notice

July 13, 2020 By SnippetPress

Footer

Recent

  • Change The WordPress Page Title Separator Without A Plugin
  • WooCommerce – Prevent Product Being Added To Cart If A Specific Product Is Already In Cart
  • WooCommerce – Display an “Add $X To Get Free Shipping” Notice
  • WooCommerce – Integrate Google Merchant Center Customer Reviews
  • WooCommerce – Disable Out of Stock Variations

Search

Copyright © 2022 ·SnippetPress