beninabox_uk
beninabox_uk

Reputation: 684

Changing out of stock text on variable products

I'm creating a Wordpress website with Woocommerce and am using variable products. I know how to change the out of stock text for the variants of the product, but I would like to know how to change the out of stock text when I have no variants added at all (i.e. set the product type to variable product and add no variants).

As it stands, the text that is shown by default is: "This product is currently out of stock and unavailable."

How would I go about changing that text?

Demonstration

Upvotes: 3

Views: 4163

Answers (4)

Pikamander2
Pikamander2

Reputation: 8319

Here's an example that uses WooCommerce's woocommerce_out_of_stock_message hook and a placeholder condition.

function edit_woocommerce_out_of_stock_message($message)
{
    if (true)
    {
        $message = 'My custom out of stock message';
    }

    return $message;
}

add_filter('woocommerce_out_of_stock_message', 'edit_woocommerce_out_of_stock_message', 10, 1);

You could then, for example, adjust the message based on the current product:

function edit_woocommerce_out_of_stock_message($message)
{
    global $product;
    
    if ($product && has_term(12, 'product_cat', $product->get_id()))
    {
        $message = 'Category 12 has been discontinued.';
    }
    
    return $message;
}

add_filter('woocommerce_out_of_stock_message', 'edit_woocommerce_out_of_stock_message', 10, 1);

Upvotes: 0

Pinguluk
Pinguluk

Reputation: 46

That message uses the woocommerce_out_of_stock_message hook. You can add a filter for it:

function custom_woocommerce_out_of_stock_message()
{
    return __('This product is currently out of stock and unavailable.', 'woocommerce');
}
add_filter('woocommerce_out_of_stock_message', 'custom_woocommerce_out_of_stock_message', 10);

and just change the text.

Upvotes: 1

LoicTheAztec
LoicTheAztec

Reputation: 254473

Another way is to use the dedicated WC filter hook woocommerce_get_availability this way:

add_filter( 'woocommerce_get_availability', 'change_specific_out_of_stock_availability', 20, 1 );
function change_specific_out_of_stock_availability( $availability ) {
    $targeted_text = ('This product is currently out of stock and unavailable.', 'woocommerce');

    if( $availability['class'] == 'out-of-stock' && $availability['availability'] == $targeted_text )
        $availability['availability'] = __('Custom "Out of stock" notice', 'woocommerce');

    return $availability;
}

This code goes on function.php file of your active child theme (or theme).

Tested and works.

Upvotes: 3

Andrew Schultz
Andrew Schultz

Reputation: 4243

Really easy to achieve this with the gettext filter.

function modify_gettext( $translated_text, $text, $domain ) {
    switch ( $translated_text ) {
        case 'This product is currently out of stock and unavailable.' :
            $translated_text = __( 'This is my new out of stock text', 'woocommerce' );
            break;
    }
    return $translated_text;
}

add_filter( 'gettext', 'modify_gettext', 20, 3 );

Upvotes: 5

Related Questions