Skip to content

Instantly share code, notes, and snippets.

@FrancoStino
Last active March 17, 2023 15:17
Show Gist options
  • Save FrancoStino/99f62f8b491863d4353e5456e9e8d4c2 to your computer and use it in GitHub Desktop.
Save FrancoStino/99f62f8b491863d4353e5456e9e8d4c2 to your computer and use it in GitHub Desktop.
Grey-out Out of Stock Variations and Disable Out of Stock Variations - Woocommerce
<?php
// Disable out of stock variations @ WooCommerce Single
add_filter( 'woocommerce_variation_is_active', 'grey_out_variations_when_out_of_stock', 10, 2 );
function grey_out_variations_when_out_of_stock( $grey_out, $variation ) {
?>
<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( 'ESAURITO', '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 );
el.css('color', 'red');
} else {
if ( el.html().match( re ) ) el.html( el.html().replace( re,'' ) );
}
} );
} );
</script>
<?php
if ( ! $variation->is_in_stock() )
return false;
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment