rachael-portier
12/31/2019 - 2:51 PM

Out of Stock Variation Dropdown

/**************************************** SINGLE PRODUCT - GREY OUT UNAVAILABLE VARIATION ****************************************/
/***** 1. GREY OUT OF STOCK VARIABLE *****/
function gray_out_variation( $active, $variation ) {
 if( ! $variation->is_in_stock() ) {
 return false;
 }
 return $active;
}
add_filter( 'woocommerce_variation_is_active', 'gray_out_variation', 10, 2 );


/***** 2. APPEND OUT OF STOCK MESSAGE TO DROPDOWN *****/
function woocommerce_sold_out_dropdown() {
  ?>
  <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( 'Out of Stock', '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>
  <?php
}
add_action( 'woocommerce_before_add_to_cart_form', 'woocommerce_sold_out_dropdown' );


/***** 3. REMOVE STOCK DISPLAY FROM SINGLE PRODUCT TEMPLATE WITH CSS *****/