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>
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
}
Thanks in advance