Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save DeveloperWil/1d51ca8d37c972a5f341e6bcd63e4f87 to your computer and use it in GitHub Desktop.

Select an option

Save DeveloperWil/1d51ca8d37c972a5f341e6bcd63e4f87 to your computer and use it in GitHub Desktop.
WooCommerce: Disable out-of-stock product variations from showing in the drop-down front-end UI add "Sold Out" label
/**
* Finds product varant option dropdown with " - sold out" and adds a "sold-out" class to the option.
* You can add the following CSS to grey-out the sold out option
* option.sold-out{ color:lightgray;}
*/
jQuery(window).load(function(){
jQuery('body.single-product .product.type-product table.variations td.value select option').each(function( index ){
var optionText = jQuery(this).text();
if( optionText.indexOf(" - sold out") >= 0 ){
jQuery(this).addClass( 'sold-out' );
}
});
});
/**
* Disable out-of-stock variations
*
* Make sure you check "Manage Stock" on each variation.
* Set the stock level to zero and in the front-end drop-down variations list
* the variation will be greyed-out on the product drop-down
*
* @author Wil Brown zeropointdevelopment.com
* @param $active
* @param $variation
*
* @return boolean
*/
function zpd_variation_is_active( $active, $variation ) {
if( ! $variation->is_in_stock() ) {
return false;
}
return $active;
}
add_filter( 'woocommerce_variation_is_active', 'zpd_variation_is_active', 10, 2 );
/**
* Adds " - sold out" to the drop-down list for out-of-stock variatons.
*
* Make sure you check "Manage Stock" on each variation.
* Set the stock level to zero and in the front-end drop-down variations list
*
* @param $option
* @param $_
* @param $attribute
* @param $product
*
* @return mixed|string
*/
function zpd_add_sold_out_label_to_wc_product_dropdown( $option, $_, $attribute, $product ){
if( is_product() ){
$sold_out_text = ' - sold out';
$variations = $product->get_available_variations();
$attributes = $product->get_attributes();
$attribute_slug = zpd_wc_get_att_slug_by_title( $attribute, $attributes );
if( empty( $attribute_slug ) ) return $option;
foreach ( $variations as $variation ) {
if ( $variation['attributes']['attribute_' . $attribute_slug] === $option && $variation['is_in_stock'] === FALSE ) {
$option .= $sold_out_text;
}
}
}
return $option;
}
add_filter( 'woocommerce_variation_option_name', 'zpd_add_sold_out_label_to_wc_product_dropdown', 1, 4 );
/**
* Returns the slug of the WooCommerce attribute taxonomy
*
* @param $attribute_title
* @param $attributes
*
* @return int|string
*/
function zpd_wc_get_att_slug_by_title( $attribute_title, $attributes ){
if ( empty( $attribute_title ) || empty( $attributes )) __return_empty_string();
$att_slug = '';
foreach( $attributes as $tax => $tax_obj ){
if( $tax_obj[ 'name'] === $attribute_title ){
$att_slug = $tax;
}
}
return $att_slug;
}
@DeveloperWil
Copy link
Author

DeveloperWil commented Jul 31, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment