yogi_bear
yogi_bear

Reputation: 73

WooCommerce added to cart message notification text


I've been struggling all day to figure out how to add HTML content to the WooCommerce "added to cart" notification message. Per default it just displays plain text.

FYI - the actual text is translated in the WooCommerce .po file, to look like this:

<h5>added</h5><strong>%s</strong> has been added to your basket.

I have figured out to use the below snippet to convert the output to HTML, but I cannot figure out how to add it to the child theme functions.php

$message .= "Content-Type: text/html; charset=UTF-8\r\n";

I am able to use it in this context, but would prefer to just pull the text from the .po file:

add_filter( 'wc_add_to_cart_message_html', 'custom_add_to_cart_message' );
function custom_add_to_cart_message() {
    $message .= "Content-Type: text/html; charset=UTF-8\r\n";
    $message = '<h5>added</h5>Product has been added to your basket.' ; 
    return $message;
}


My php knowledge is at a bare minimum and I would really appreciate some feedback on how to implement the basic html changing code - add_filter / add_action or something similar.

Upvotes: 0

Views: 5837

Answers (1)

Dmitry Leiko
Dmitry Leiko

Reputation: 4412

You can try code below:

function custom_add_to_cart_message() {
    $return_to  = get_permalink(wc_get_page_id('shop'));

    if (get_option('woocommerce_cart_redirect_after_add')=='yes') {
        $message = sprintf('<a href="%s" class="button">%s</a> %s', get_permalink(wc_get_page_id('cart')), __('View Cart &rarr;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
    } else {
        $message = sprintf('<a href="%s" class="button">%s</a> %s', $return_to, __('Continue Shopping &rarr;', 'woocommerce'), __('Product successfully <h5>added</h5>to your cart.', 'woocommerce') );
    }

    return $message;
}
add_filter( 'wc_add_to_cart_message', 'custom_add_to_cart_message' );

I checked it's work fine for me. Hope help you.

Upvotes: 1

Related Questions