I am trying to alter messages displayed when adding an a product to cart and/ or updating the cart by hooking in to the woocommerce_add_message
. It's not showing anything at all and I'm wondering why.
I've tried echo
and I've tried return__(
Here's the code:
add_filter('woocommerce_add_message', 'change_cart_message', 10);
function change_cart_message() {
$ncst = WC()->cart->subtotal;
if ( is_checkout() ) {
echo 'Your new order subtotal is: '.$ncst.'. <a style="color: green;" href="#customer_details">Ready to checkout?</a>';
}
elseif ( is_product() ) {
echo 'Your new order subtotal is: '.$ncst.'. <a style="color: green;" href="'.wc_get_checkout_url().'">Ready to checkout?</a>';
}
else {
echo 'Your new order subtotal is: '.$ncst.'. <a style="color: green;" href="'.wc_get_checkout_url().'">Ready to checkout?</a>';
}
}
What am I doing wrong?
When using a filter hook, you need always to return the filtered value argument (but not to echo it)…
Also your code can be simplified and compacted:
Code goes in function.php file of your active child theme (or active theme). Tested and works.