1

I would like to rename 'Add to Cart' to 'Sold Out' when product is sold out in WooCommerce.

This is my code:

add_filter( 'add_to_cart_text', 'woo_custom_single_add_to_cart_text' );
add_filter( 'woocommerce_product_single_add_to_cart_text', 
'woo_custom_single_add_to_cart_text' );  // 2.1 +

function woo_custom_single_add_to_cart_text() {

if( $product->get_stock_quantity() == 0 )
    return __( 'Sold Out', 'woocommerce' );
} else {
    return __( 'Add to cart ', 'woocommerce' );
}

But it's not working.

What I am doing wrong please?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
angga
  • 89
  • 3
  • 9

2 Answers2

3

UPDATE (Working now with variable products and variations)

There is some errors in your code:

1.You need to to set the hooks arguments in your function as $button_text and $product. 2.The add_to_cart_text is deprecated and replaced by woocommerce_product_add_to_cart_text.

In this updated code I have added a jQuery script that catch the selected variation for variable products only (on single product pages) and to change the button text. Now this is working for all cases.

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;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This code is tested and works

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Nice solution! But how to change onclick action of sold out button? (e.g. redirect to some custom page with contact form)? – Anton Eregin Aug 27 '19 at 17:34
  • This solution still works, big thumbs up. However I think that is worth mentioning that for looping simple products in related.php it is not working since they are usually included in a single product page. I resolved it by adding another condition `elseif ( ! $product->is_type('variable') && is_product() ) :` it works in my scenario. – ulimc Feb 10 '21 at 14:14
0

It looks like you forgot to declare $product as a global.

Your filter function should start with global $product;

Niek van der Steen
  • 1,413
  • 11
  • 33