Woocommerce, How to edit the message "added to cart"

When you click the Add to Cart button, Woocommerce shows the message, looks at the basket, I want to edit this message, actually edit the entire range, put the icon, etc.

+7
wordpress e-commerce hook edit woocommerce
source share
7 answers

Add a filter to your /functions.php theme. The code below simply overrides the existing $ message. This overwrites the $ message with an almost identical message, which adds a message link to the message.

Make sure you return the message $.

You can, of course, just change the existing message, because the whole thing is passed as a string through the first parameter or $ message var.

add_filter ( 'wc_add_to_cart_message', 'wc_add_to_cart_message_filter', 10, 2 ); function wc_add_to_cart_message_filter($message, $product_id = null) { $titles[] = get_the_title( $product_id ); $titles = array_filter( $titles ); $added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) ); $message = sprintf( '%s <a href="%s" class="button">%s</a>&nbsp;<a href="%s" class="button">%s</a>', esc_html( $added_text ), esc_url( wc_get_page_permalink( 'checkout' ) ), esc_html__( 'Checkout', 'woocommerce' ), esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' )); return $message; } 
+10
source share

You have tried the filter as shown below.

 function your_add_to_cart_message() { if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) : $message = sprintf( '%s<a href="%s" class="your-style">%s</a>', __( 'Successfully added to cart.', 'woocommerce' ), esc_url( get_permalink( woocommerce_get_page_id( 'shop' ) ) ), __( 'Continue Shopping', 'woocommerce' ) ); else : $message = sprintf( '%s<a href="%s" class="your-class">%s</a>', __( 'Successfully added to cart.' , 'woocommerce' ), esc_url( get_permalink( woocommerce_get_page_id( 'cart' ) ) ), __( 'View Cart', 'woocommerce' ) ); endif; return $message; } add_filter( 'wc_add_to_cart_message', 'your_add_to_cart_message' ); 

In response to updating the ajax post, try the translation function, for example:

 function your_woo_ajax_solution( $translation, $text, $domain ) { if ( $domain == 'woocommerce' ) { // your domain name if ( $text == 'View Cart' ) { // current text that shows $translation = 'Basket updated.'; // The text that you would like to show } } return $translation; } add_filter( 'gettext', 'your_woo_ajax_solution', 10, 3 ); 
+4
source share

If you look at add-to-cart.js , it fires the added_to_cart trigger when a product is added to the cart. I connected to it and did it

 jQuery(document.body).on("added_to_cart", function( data ) { jQuery('button.added').nextAll().remove(); jQuery('button.added').after(' <span style="text-align:center;display:block;" class="cart_updated_ajax"><a href="' + wc_add_to_cart_params.cart_url + '" title="' + wc_add_to_cart_params.i18n_view_cart + '">Cart Updated</a></span>'); }); 

Here you can add something after adding the product to the cart.

Hope this helps!

+1
source share

In Woocommerce 3.0, "wc_add_to_cart_message" is deprecated and no longer works. So when the answer from @zmonteca was ok, it no longer works with Woocommerce 3.0

Just replace "wc_add_to_cart_message" with " wc_add_to_cart_message_html " and the voile ... file works.

 add_filter ( 'wc_add_to_cart_message', 'wc_add_to_cart_message_filter', 10, 2 ); function wc_add_to_cart_message_filter($message, $product_id = null) { $titles[] = get_the_title( $product_id ); $titles = array_filter( $titles ); $added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', sizeof( $titles ), 'woocommerce' ), wc_format_list_of_items( $titles ) ); $message = sprintf( '%s <a href="%s" class="button">%s</a>&nbsp;<a href="%s" class="button">%s</a>', esc_html( $added_text ), esc_url( wc_get_page_permalink( 'checkout' ) ), esc_html__( 'Checkout', 'woocommerce' ), esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View Cart', 'woocommerce' )); return $message;} 
0
source share

@Dante is correct, the solutions provided by @BradleyD will not work for ajax_add_to_cart on the store page.

The solution provided by @Abstract works as expected. I also use his solution.

Another jQuery approach is to listen for the ajaxSuccess event on the document object and make the necessary changes for the pressed button.

Something like this should work:

 $(document).ajaxSuccess(function(event, xhr, settings) { if (settings.url.indexOf('?wc-ajax=add_to_cart') !== -1) { // You can find the clicked button element under the event.target.activeElement // Than you can do whatever you want here. Add new html element and text, etc. } }); 
0
source share

2017 - 2019 - For Woocommerce 3+ (processing of multiple products has been added to the cart)

wc_add_to_cart_message_html filter hook wc_add_to_cart_message_html , the argument of the 2nd function was changed to $products (instead of $product_id ) ...

You can make changes to the code inside this connected function, as in this thread :

 add_filter( 'wc_add_to_cart_message_html', 'custom_add_to_cart_message_html', 10, 2 ); function custom_add_to_cart_message_html( $message, $products ) { $titles = array(); $count = 0; foreach ( $products as $product_id => $qty ) { $titles[] = ( $qty > 1 ? absint( $qty ) . ' &times; ' : '' ) . sprintf( _x( '&ldquo;%s&rdquo;', 'Item name in quotes', 'woocommerce' ), strip_tags( get_the_title( $product_id ) ) ); $count += $qty; } $titles = array_filter( $titles ); $added_text = sprintf( _n( '%s has been added to your cart.', '%s have been added to your cart.', $count, 'woocommerce' ), wc_format_list_of_items( $titles ) ); // The custom message is just below $added_text = sprintf( _n("%s item has %s", "%s items have %s", $count, "woocommerce" ), $count, __("been added to your basket.", "woocommerce") ); // Output success messages if ( 'yes' === get_option( 'woocommerce_cart_redirect_after_add' ) ) { $return_to = apply_filters( 'woocommerce_continue_shopping_redirect', wc_get_raw_referer() ? wp_validate_redirect( wc_get_raw_referer(), false ) : wc_get_page_permalink( 'shop' ) ); $message = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ), esc_html( $added_text ) ); } else { $message = sprintf( '<a href="%s" class="button wc-forward">%s</a> %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ), esc_html( $added_text ) ); } return $message; } 

Related topics (for Woocommerce 3+):

  • Hide Woocommerce message added to cart
  • Customize Cart Add Messages Based on Product Identifiers in WooCommerce 3
  • Customize add to cart message in Woocommerce 3
0
source share

To change this, go to the woocommerce plugin, then follow this path:

/includes/class-wc-frontend-scripts.php line number: 157

-7
source share

All Articles