I want to add a feature for the variables that out of stock to disable them and add a “out of stock” suffix so that when users select the variables, they will know which variable has an existing variable and they can select only that one and the others will be disabled and that out of stock suffix as well. be shown next to the variable name.
The code I edited is as follows, but it only disables the variables and does not show the “out of stock” suffix.
// PHP function to disable out-of-stock variations and inline jQuery script
add_filter('woocommerce_variation_is_active', 'bbloomer_grey_out_variations_out_of_stock', 10, 2);
function bbloomer_grey_out_variations_out_of_stock($is_active, $variation) {
if (!$variation->is_in_stock()) return false;
return $is_active;
}
// Inline jQuery script
add_action('wp_footer', 'custom_inline_jquery_script');
function custom_inline_jquery_script() {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
$(window).load(function() {
$('body.single-product .product.type-product table.variations td.value select option').each(function(index) {
var optionText = $(this).text();
if (optionText.indexOf(" - sold out") >= 0) {
$(this).addClass('sold-out');
}
});
});
});
</script>
<?php
}