Skip to content

Instantly share code, notes, and snippets.

@jackgregory
Last active December 15, 2015 22:58
Show Gist options
  • Save jackgregory/5336411 to your computer and use it in GitHub Desktop.
Save jackgregory/5336411 to your computer and use it in GitHub Desktop.
WooCommerce - Disable and add 'sold out' to a single WooCommerce product variation dropdown
add_action( 'woocommerce_before_add_to_cart_form', 'woocommerce_variations_sold_out_filter' );
function woocommerce_variations_sold_out_filter() {
// disable and add "sold out" to product variations
wc_enqueue_js( '
var product_variations = $( "form.variations_form" ).data( "product_variations" );
// dont disable with multiple drop-downs
if ( product_variations && $( "form.variations_form" ).find( "select" ).length === 1 ) {
var attribute_name = $( "form.variations_form" ).find( "select" ).attr( "name" );
$.each( product_variations, function( key, value ) {
if ( !value.is_in_stock ) {
var $variation = $( "form.variations_form" ).find( "option[value=\'" + value.attributes[ attribute_name ] + "\']" ),
variation_text = $variation.text();
$variation.text( variation_text + " - ' . esc_js( __( 'Sold Out', 'woocommerce' ) ) . '" ).attr( "disabled", "disabled" );
}
} );
}
' );
}
@jackgregory
Copy link
Author

@j-niell try hooking the above action to the wp_footer e.g.

add_action( 'wp_footer', 'woocommerce_variations_sold_out_filter' );

@meumundofit
Copy link

Hi, this is not working on WooCommerce 2.0+. Is there a way to fix it?
thanks

@cfrtb
Copy link

cfrtb commented Dec 3, 2014

I just used the code that aplatform commented on for April 1st. And it works just fine. Sooooooo awesome!!! In functions file (or includes file if that is what you choose to use, place):

add_action( 'wp_footer', 'woocommerce_sold_out_filter' );
function woocommerce_sold_out_filter() {
?>
<script type="text/javascript">
(function($) {
// disable and add 'sold out' to product variations
var product_variations = $('form.variations_form').data('product_variations');
if (product_variations) {
var attribute_name = $('form.variations_form').find('select').attr('name');
$.each(product_variations, function(key, value) {
if (!value.is_in_stock) {
var variation_text = $(".variations option[value='" + value.attributes[attribute_name] + "']").text();
$(".variations option[value='" + value.attributes[attribute_name] + "']").attr('disabled',     'disabled').text(variation_text + ' - Sold Out');
}
});
}
})(jQuery);
</script><?php
} 

Oh, and make sure you go into your woocommerce settings->products->inventory and that the box for hide sold out items is unchecked.

@MysticsWeb
Copy link

Thank you so much for this I have been looking everywhere for code that works!
However it doesn't seem to work so well when you have multiple variations like colour and then size. Say I have 3 sizes of the one colour and only 1 size is available it will show sold out 2 times but not let me see the last size available. Anyone have a fix for this? Would you like me to post a link?

@navalon
Copy link

navalon commented Feb 2, 2015

You are my hero!!!!

@jackgregory
Copy link
Author

@MysticsWeb little late on this, but it doesn't work on multiple variations or drop-downs. Ive updated the gist to reflect this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment