Add ‘Sold Out’ to WooCommerce Variable Product Dropdown

This is a quick little ‘how to’ that came up in a recent client WooCommerce request. The client wanted to add the text ‘sold out’ to the variable product configuration dropdowns on the product page to make it more clear to customers when a variation was out of stock:

Before and After
The Implementation
This was a bit more difficult to implement than you might imagine at first blush, because the product variation dropdowns are controlled by somewhat tricky JavaScript. The relevant JavaScript can be found in the file woocommerce/assets/js/add-to-cart-variation.js in an anonymous function callback which is invoked every time the page loads, or the variable product is configured by the user, and ensures that the correct product options are displayed, and enabled or disabled. Luckily for us this function fires a custom DOM event named ‘woocommerce_update_variation_values’, allowing us to listen to changes and manipulate the dropdowns to add our custom text.
The JavaScript we need to inject into the page to add the ‘Sold Out’ (or any other notice) is the following:
<script type="text/javascript">
jQuery(document).bind('woocommerce_update_variation_values', function() {
jQuery('.variations select option').each( function(index, el) {
var sold_out = '<?php _e( 'sold out', 'woocommerce' ); ?>';
var re = new RegExp(' - ' + sold_out + '$');
el = jQuery(el);
if(el.is(':disabled')) {
if(!el.html().match(re)) el.html(el.html() + ' - ' + sold_out);
} else {
if(el.html().match(re)) el.html(el.html().replace(re,''));
}
});
});
</script>
This binds to the ‘woocommerce_update_variation_values’ event, and loops through the variation options, checking for disabled options and adding in the ‘sold out’ text if needed, and removing the ‘sold out’ text from the active options if needed. Of course this solution would fail if your product option name ended with the string ” – sold out”, but that would just be silly.
The final step is to inject this javascript into the page content via the ‘woocommerce_before_add_to_cart_form’ action like so:
add_action( 'woocommerce_before_add_to_cart_form', 'woocommerce_sold_out_dropdown' );
function woocommerce_sold_out_dropdown() {
?>
// JavaScript from above goes here
<?php
}
Add this to your own Custom Plugin, or into your theme functions file. Note: in order for the procedure described within this article to work, you must go to WooCommerce > Settings > Inventory, and ensure that “Hide out of stock items from the catalog” is checked. Without that option enabled the results will be less than impressive. Also, any products that you want to enable this on must be configured to not allow backorders: edit the product and go to Product Data > Inventory and set Allow Backorders? to “Do not allow”. If you use this basic recipe to customize the variable product dropdowns in some other new or creative way, let us know in the comments section below!
GREAT JOB ! works perfectly! i simply insterted the code into my woocommerce-functions.php and worked perfectly!
Awesome! Glad to hear it
Works like a charm…thank you!
Glad to hear it, you’re welcome!
hi
is there a way to stop the variations from showing in the drop down menu when their stock value is 0
i really need you help as the client want to launch this Friday and i can not find a way round
THANK YOU! Worked like a charm.
Great! Glad to hear it
This isn’t working for me.
I have the latest version of WooCommerce which is supposed to show the stock on the product page when a variation is selected.
What’s strange is that when I look in variations.php, there seems to be no code which adds the disabled attribute to the options.
if ( taxonomy_exists( sanitize_title( $name ) ) ) {
$terms = get_terms( sanitize_title($name), array(‘menu_order’ => ‘ASC’) );
foreach ( $terms as $term ) {
if ( ! in_array( $term->slug, $options ) ) continue;
echo ‘slug . ‘” ‘ . selected( $selected_value, $term->slug, false ) . ‘>’ . apply_filters( ‘woocommerce_variation_option_name’, $term->name ) . ”;
}
} else {
foreach ( $options as $option )
echo ” . apply_filters( ‘woocommerce_variation_option_name’, $option ) . ”;
}
Hey Sebastien, I know that WooCommerce updated their variation handling javascript in a recent release, so I just tested my old solution out with the latest version of WooCommerce and it worked just fine. You’re correct in your observation that variations.php doesn’t disable any product variations. As I describe in the article, that’s part of what makes altering the variation handling code a little bit tricky: it’s all done with some complex JavaScript found within add-to-cart-variation.js, and so is implemented in the frontend, rather than in PHP code, which is a perfectly understandable place to start looking.
Is it possible for me to achieve the same result without having hide the product on catalog page?
This particular example relies on that setting being enabled, it’s how the out of stock items are detected. That being said, almost anything can be done, it would just require a bunch more coding and a slightly different approach.
what about if there are two options, say, size and color.
I need a solution so if the BLUE MEDIUM shirt is not available… uses can still choose BLUE but when they go to choose SIZE MEDIUM will show as not avilable, but small and large will show as available
doabale?
Hello I am trying to get this to work.. it’s working somewhat.. the out of stock variables are greyed out but the text “sold out” is not showing up. Is there something that I missed?
also only some of my products are showing the “in stock” text” etc.
No, actually I think I had part of the code slightly wrong. I’ve updated it so you can try again and if it works for you let me know
Thanks, Justin. I just started playing around with this and it works in most cases. However, I discovered there are reasons an item could be disabled besides being sold out, and then they get mislabeled.
For example, let’s say you have a book for sale with a media type attribute having print or e-book options, and a language attribute having English or Spanish options. Both attributes are used for variations. But the Spanish version is only available as an e-book, so the print variation is left out.
Then, if someone selects print in the media type drop-down menu, the language menu will show Spanish as disabled, which would then incorrectly get the sold out text added.
I haven’t come up with a way yet to alter this behavior since we can’t tell why an item is disabled. Any ideas?
hmm, yeah good point. Honestly it may not even be possible to detect these sorts of cases, the javascript that handles this stuff is not the easiest to work with. It would take a bit of research and digging to determine
Just what I needed for an upcoming project. thanks!
Hi Justin!
I can’t see the Javascript code of the first box:
http://img35.imageshack.us/img35/1157/capturadepantalla201305ek.png
I’ve tested in Safari and Chrome.
Regards!
fixed that now, thanks for the heads up
hi all,
I want to create a custom field for woocommerce product page in the backend and the front end.if the product is for sale then it should show all the details like add to cart and all.
if the product is not for sold then it should just display the image and the simple text.Can Some one help me out soon please.
Hey, I guess your best bet would be to give the WooCommerce Product Addons plugin a try
Still can’t see the javascript box!
fixed, thanks for the heads up