Matthew John Dougall
Matthew John Dougall

Reputation: 11

Change specific text notice in WooCommerce

I would like to change the alert which displays after a user adds a product which is no longer available for purchase on the website. The error currently shows:

{product} has been removed from your basket because it can no longer be purchased. Please contact us if you need assistance.

How can I change this to a custom message?

I have tried looking for solutions which do not include installing a 3rd party plugin

Upvotes: 1

Views: 476

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253939

Try the following to change your WooCommerce notice:

add_filter( 'woocommerce_add_notice', 'change_woocommerce_notice' );
function change_woocommerce_notice( $message ) {
    // The partial text of the notice to find
    $partial_text = "has been removed from your basket because it can no longer be purchased. Please contact us if you need assistance.";

    if ( $message strpos( $message, $partial_text ) !== false ) {
        // Extract the product name (if you want to use it)
        $product_name = str_replace( $partial_text, '', $message );

        // Your replacement message
        $message = __("This is your replacement message", "woocommerce");
    }   
    return $message;
}

Code goes in functions.php file of your active child theme (or active theme). It should work.

Upvotes: 0

Related Questions