-
-
Save jackgregory/5336411 to your computer and use it in GitHub Desktop.
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" ); | |
} | |
} ); | |
} | |
' ); | |
} |
@danielucas Your right, this was a quick method. Ive updated the code, let me know if this works for you.
Hi @aplatform, thanks for providing this code - I've been looking for it everywhere. I've tried to implement the latest revision into my functions.php file, but it seems it's not working. Could I have you take a look at it to see if I'm implementing it correctly?
I've played with options such as switching the attributes between name and custom ordering, but it doesn't seem to make a difference. I must be missing something in the code.
Thank you for your time.
@j-niell try hooking the above action to the wp_footer e.g.
add_action( 'wp_footer', 'woocommerce_variations_sold_out_filter' );
Hi, this is not working on WooCommerce 2.0+. Is there a way to fix it?
thanks
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.
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.
this only works if the attributes are ordered by name, if they use custom ordering or are ordered by ID the code disables the wrong variation. trying to write a fix, but I'm not very skilled with jquery...
great bit of code though.