I am looking for a solution to a problem I have...
I have products in Woocommerce, with variations. If ALL variations are out of stock, I want to change the Add to Cart button text to say "Sold out" and edit the CSS of the button also (change it's color) BEFORE a variation is selected in the dropdown...
So here's a scenario:
I goto a variable product single page. The product has 4 variations:
CURRENTLY: The "add to cart" button displays (greyed out) and can be clicked before a variation is selected. An alert appears telling the user to select a variation. When I select a variation from dropdown, the button is greyed out if out of stock in that variation. If all 4 variations are out of stock, the initial load of the page still shows add to cart button greyed out and on click says to select a variation.
WHAT I WANT: If there is AT LEAST one variation still in stock, the standard Woocommerce functionality stays (Add To Cart visible, with alert popping up when clicked to tell them to select a variation). If NO variations are in stock, the Add to Cart button says "sold out" straight away, and is greyed out. (Before I select a variation).
The problem I'm finding is that all the existing code out there to change the add to cart button text is done when a variation is selected from the dropdown. I somehow need to check if ANY of the variations are in stock, (before they are selected) and then either change the button text to "sold out" if ALL variations are out of stock, or leave it at first load, and change the text only when the out of stock variation is selected.
I hope that's clear. Thanks!
===
As requested, I've tried the following code:
add_filter( 'woocommerce_product_add_to_cart_text',
'customizing_add_to_cart_button_text', 10, 2 );
add_filter( 'woocommerce_product_single_add_to_cart_text',
'customizing_add_to_cart_button_text', 10, 2 );
function customizing_add_to_cart_button_text( $button_text, $product ) {
$sold_out = __( "Sold Out", "woocommerce" );
$availability = $product->get_availability();
$stock_status = $availability['class'];
// Only for variable products on single product pages
if ( $product->is_type('variable') && is_product() )
{
?>
<script>
jQuery(document).ready(function($) {
$('select').blur( function(){
if( '' != $('input.variation_id').val() && $('p.stock').hasClass('out-of-stock') )
$('button.single_add_to_cart_button').html('<?php echo $sold_out; ?>');
else
$('button.single_add_to_cart_button').html('<?php echo $button_text; ?>');
console.log($('input.variation_id').val());
});
});
</script>
<?php
}
// For all other cases (not a variable product on single product pages)
elseif ( ! $product->is_type('variable') && ! is_product() )
{
if($stock_status == 'out-of-stock')
$button_text = $sold_out.' ('.$stock_status.')';
else
$button_text.=' ('.$stock_status.')';
}
return $button_text;
}
This changes the buton text, but only when variations are selected - I need to check if all variations are out of stock and then change the text straight away.