I’m having an issue trying to change the output of a message via my themes functions.php file.
Here is tbe code provided by the woocommerce plugin:
function woocommerce_add_to_cart_message() {
global $woocommerce;
if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
$return_to = (wp_get_referer()) ? wp_get_referer() : home_url();
$message = sprintf('<a href="%s" class="button">%s</a> %s', $return_to, __('Continue Shopping;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
else :
$message = sprintf('<a href="%s" class="button">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __('View Cart →', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
endif;
$woocommerce->add_message( apply_filter('woocommerce_add_to_cart_message', $message) );
}
What I’m trying to do is change the button message, but I’m not exactly sure how to do this.
I’ve been reading the WP codex, but unfortunately I don’t think I quite understand how this works. Here’s what I’ve tried:
function remove_woocommerce_add_to_cart_message() {
remove_filter('woocommerce_add_to_cart_message', $message) ;
}
function woocommerce_add_to_cart_message_edited() {
global $woocommerce;
if (get_option('woocommerce_cart_redirect_after_add')=='yes') :
$return_to = (wp_get_referer()) ? wp_get_referer() : home_url();
$message = sprintf('<a href="%s" class="button">%s</a> %s', $return_to, __('Continue Shopping;', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
else :
$message = sprintf('<a href="%s" class="button">%s</a> %s', get_permalink(woocommerce_get_page_id('cart')), __('NEW CART MESSAGE', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
endif;
$woocommerce->add_message( apply_filter('woocommerce_add_to_cart_message', $message) );
}
So, I’ve tried removing the filter that outputs the message, then I declare it, but it doesn’t seem to work.
Any help would be appreciated it!
When the code states
apply_filter('woocommerce_add_to_cart_message', $message)
the code will look for any filter that is added to that hook, and go through them one at a time.You can create a function, named whatever you like, and add it to that hook like so:
the above code I have made changes in it please check may be it is helpful to you