Reputation: 383
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!
Upvotes: 1
Views: 6711
Reputation: 45
// Output success messages
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :
$return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wp_get_referer() ? wp_get_referer() : home_url() );
$message = sprintf('%s',$added_text);
else :
$message = sprintf('%s',$added_text);
endif;
the above code I have made changes in it please check may be it is helpful to you
Upvotes: 0
Reputation: 3186
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:
function my_filer_function( $message )
{
// Here you should modify $message as you want, and then return it.
$newButtonString = 'NEW BUTTON STRING';
$replaceString = '<a$1class="button">' . $newButtonString .'</a>';
$message = preg_replace('#<a(.*?)class="button">(.*?)</a>#', $replaceString, $message);
return $message;
}
// Then add the function to that filter hook and prioritize it last
add_filter( 'woocommerce_add_to_cart_message', 'my_filer_function', 999);
Upvotes: 2