Last active
December 15, 2015 22:58
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" ); | |
} | |
} ); | |
} | |
' ); | |
} |
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?
You are my hero!!!!
@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
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):
Oh, and make sure you go into your woocommerce settings->products->inventory and that the box for hide sold out items is unchecked.