0
votes

In Woocommerce I am trying to change the add to cart text from "Add to cart" to "Out of stock" on single product pages when the product is out of stock, for simple products and the product variations on variable products.

We are currently not using any sort of stock management because all of our products are hand made & we do set stock status manually.

Is there any possible way to achieve that?

2

2 Answers

0
votes

For all products except variable products, is quite simpleā€¦ But for variable products and it's variations, it requires some more code and the usage of javascript/jQuery.

So the following code will change add to cart text to "Out of stock" when the current product is out of stock even for selected product variations on variable products:

// For all products except variable product
add_filter( 'woocommerce_product_single_add_to_cart_text', 'product_single_add_to_cart_text_filter_callback', 20, 2 );
function product_single_add_to_cart_text_filter_callback( $button_text, $product ) {
    if( ! $product->is_in_stock() && ! $product->is_type('variable') ) {
        $button_text = __("Out of stock", "woocommerce");
    }
    return $button_text;
}

// For all product variations (on a variable product)
add_action( 'woocommerce_after_add_to_cart_button', 'after_add_to_cart_button_action_callback', 0 );
function after_add_to_cart_button_action_callback() {
    global $product;

    if( $product->is_type('variable') ) :

    $data = [];

    // Loop through variation Ids
    foreach( $product->get_visible_children() as $variation_id ){
        $variation = wc_get_product( $variation_id );
        $data[$variation_id] = $variation->is_in_stock();
    }

    $outofstock_text = __("Out of Stock", "woocommerce");
    ?>
    <script type="text/javascript">
    jQuery(function($){
        var b = 'button.single_add_to_cart_button',
            t = $(b).text();

        $('form.variations_form').on('show_variation hide_variation found_variation', function(){
            $.each(<?php echo json_encode($data); ?>, function(j, r){
                var i = $('input[name="variation_id"]').val();
                if(j == i && i != 0 && !r ) {
                    $(b).html('<?php echo $outofstock_text; ?>');
                    return false;
                } else {
                    $(b).html(t);
                }
            });
        });
    });
    </script>
    <?php
    endif;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

0
votes

Tried to write as comment but seems code formatting not working, so writing as an answer.

For a single variation add to cart shortcode this hack will help.

// For variation product
    add_filter( 'woocommerce_product_add_to_cart_text', 'product_variation_add_to_cart_text_filter_callback', 20, 2 );
    function product_variation_add_to_cart_text_filter_callback( $button_text, $product ) {
        if ( ! $product->is_in_stock() && $product->is_type( 'variation' ) ) {
            $button_text = __( "Out of stock", "woocommerce" );
        }

        return $button_text;
    }