The below is what I’m using at the minute, if I can not display soldout and have something else when stock is in a cart.
Thanks,
Dean.
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”);
$incart = __( “Already in Cart”, “woocommerce”);
$addcart = __( “Add to Cart”, “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; ?>’);
$(‘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;
return $button_text;}
else
$cartidnum = 0;
foreach( WC()->cart->get_cart() as $values ) {
$products_ids_array[] = $values[‘product_id’];
$_product = $values[‘product_id’];
if( $product->get_id() == $products_ids_array[$cartidnum] ) {
$button_text = $incart;
return $button_text;
}
else
$cartidnum++;
}
}
$button_text = $addcart;
return $button_text;
}