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" );
}
} );
}
' );
}
@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