When click on add to cart button, the Woocommerce shows the message, view cart, I want to edit this message, actually edit all the span, put some icon etc...
Add a filter to your theme/functions.php. The code below just overrides the existing $message. This overwrites $message with an nearly identical one that prepends a "checkout" link to the message.
Make sure you return the $message.
You can of course just modify the existing message, as the entire thing is passed as a string via the first param 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 %s %s',
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;
}
Have you tried a filter like the following
function your_add_to_cart_message() {
if ( get_option( 'woocommerce_cart_redirect_after_add' ) == 'yes' ) :
$message = sprintf( '%s%s', __( 'Successfully added to cart.', 'woocommerce' ), esc_url( get_permalink( woocommerce_get_page_id( 'shop' ) ) ), __( 'Continue Shopping', 'woocommerce' ) );
else :
$message = sprintf( '%s%s', __( '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 reply to the ajax message update, try a translation function like:
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 );
2017 - 2019 - For Woocommerce 3+ (handling multiple products added to cart)
Replaced by wc_add_to_cart_message_html filter hook, the 2nd function argument has changed to $products (instead of $product_id)…
You can make changes on the code inside this hooked function, like 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 ) . ' × ' : '' ) . sprintf( _x( '“%s”', '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( '%s %s', esc_url( $return_to ), esc_html__( 'Continue shopping', 'woocommerce' ), esc_html( $added_text ) );
} else {
$message = sprintf( '%s %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ), esc_html( $added_text ) );
}
return $message;
}
Related threads (for Woocommerce 3+):
Hide Added to Cart message in Woocommerce
Customizing add-to-cart messages based on the product IDs in WooCommerce 3
Customize add to cart message in Woocommerce 3
If you look at add-to-cart.js it fires a trigger added_to_cart on adding a product to cart. I hooked into that and did this
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 anything after product is added to cart.
Hope that helps!
In Woocommerce 3.0 "wc_add_to_cart_message" is obsolete and no longer works. So while the answer by #zmonteca was ok, is no longer working on the Woocommerce 3.0
Just replace "wc_add_to_cart_message" with "wc_add_to_cart_message_html" and voile... 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 %s %s',
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;}
#Dante is correct, the solutions provided by #BradleyD won't work for ajax_add_to_cart on shop page.
The solution provided by #Abstract is working as expected. I am also using his solution.
Another jQuery approach is to to listen for the the ajaxSuccess event on the document object and do the desired modifications for the clicked button.
Something like that 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.
}
});
Related
I am using a css to change the price view of the product with the code below. I found this to be the only advantageous working code. However, this code also appears on the product archive page and other components. I just want it to affect the price on the product page. And in any case, prices should appear in this view.
add_filter( 'woocommerce_get_price_html', 'bbloomer_simple_product_price_format', 10, 2 );
function bbloomer_simple_product_price_format( $price, $product ) {
if ( $product->is_on_sale() && $product->is_type('simple') ) {
$price = sprintf( __( '<div class="featured-price-container clickable"><div class="featured-price-box"><div class="featured-price-info"><div class="featured-price-discount"></div>Son 30 Günün En Düşük Fiyatı!</div><div class="featured-prices"><span class="prc-org">%1$s</span><span class="prc-dsc">%2$s</span></div></div></div>', 'woocommerce' ), wc_price ( $product->get_regular_price() ), wc_price( $product->get_sale_price() ), wc_price( $product->get_regular_price() - $product->get_sale_price() ) );
}
return $price;
}```
add_filter( 'woocommerce_get_price_html', 'bbloomer_simple_product_price_format', 10, 2 );
function bbloomer_simple_product_price_format( $price, $product ) {
if ( $product->is_on_sale() && $product->is_type('simple') ) {
$price = sprintf( __( '<div class="featured-price-container clickable"><div class="featured-price-box"><div class="featured-price-info"><div class="featured-price-discount"></div>Son 30 Günün En Düşük Fiyatı!</div><div class="featured-prices"><span class="prc-org">%1$s</span><span class="prc-dsc">%2$s</span></div></div></div>', 'woocommerce' ), wc_price ( $product->get_regular_price() ), wc_price( $product->get_sale_price() ), wc_price( $product->get_regular_price() - $product->get_sale_price() ) );
}
return $price;
}
I'm using this snippet below to slightly modify Woocommerce's added to cart message. It redirects to the cart page from product pages. I'd like to add the product's thumbnail image to that Woocommerce message notification to show it clearer to the customer what has been added to the cart. Any solutions? I've tried a bunch of different ways, but no success.
function ace_add_to_cart_message_html( $message, $products ) {
$count = 0;
$titles = array();
foreach ( $products as $product_id => $qty ) {
$titles[] = ( $qty > 1 ? absint( $qty ) . ' × ' : '' ) . sprintf( _x( '“%s”', '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.', // Singular
'%s are added to your cart.', // Plural
$count, // Number of products added
'woocommerce' // Textdomain
), wc_format_list_of_items( $titles ) );
$message = sprintf( '%s %s', esc_url( wc_get_checkout_url() ), esc_html__( 'Proceed to checkout', 'woocommerce' ), esc_html( $added_text ) );
return $message;
}
You can use wc_add_to_cart_message action hook. try the below code.
function add_product_image_wc_add_to_cart_message_html( $message, $product_id ){
if ( has_post_thumbnail( $product_id ) ):
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $product_id ), 'single-post-thumbnail' );
endif;
$message = sprintf( '<img src="'.$image[0].'" style="height: 50px;width: 50px;float: left;margin-right: 10px;" />%s', $message );
return $message;
}
add_filter( 'wc_add_to_cart_message', 'add_product_image_wc_add_to_cart_message_html', 10, 2 );
Tested and Works
So I hook "woocommerce_remove_cart_item" then from here, I remove a second item from cart and add a "notice" to restore it, so I got two notices to restore items, but:
Will be possible only one notice(showing both items) and restore both items with only one click ?
// Delete second item if CONDITION
foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
$product = $cart_item['data'];
if ( CONDITION ) {
array_push($array_to_delete,$cart_item);
}
}
//Unset all cart items selected
foreach( $array_to_delete as $cart_item ) {
$product = $cart_item['data'];
$prod_unique_id = WC()->cart->generate_cart_id( $product->id );
// Remove it from the cart by un-setting it
unset( WC()->cart->cart_contents[$prod_unique_id] );
//Add undo notice
$product = wc_get_product( $cart_item['product_id'] );
$item_removed_title = apply_filters( 'woocommerce_cart_item_removed_title', $product ? sprintf( _x( '“%s”', 'Item name in quotes', 'woocommerce' ), $product->get_name() ) : __( 'Item', 'woocommerce' ), $cart_item );
$removed_notice = sprintf( __( '%s removed.', 'woocommerce' ), $item_removed_title );
$removed_notice .= ' ' . __( 'Undo?', 'woocommerce' ) . '';
wc_add_notice( $removed_notice );
*/
}
woocommerce how to add images at "has been added to your cart"?
go to your
wordpress\wp-content\plugins\woocommerce\includes\wc-cart-functions.php
find the code
} else {
$message = sprintf( '%s %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ), esc_html( $added_text ) );
}
replace it with below code
} else {
$product_meta = get_post_meta($product_id);
$img = wp_get_attachment_image(($product_meta['_thumbnail_id'][0]), 'thumbnail');
$message = sprintf( "$img".'%s %s', esc_url( wc_get_page_permalink( 'cart' ) ), esc_html__( 'View cart', 'woocommerce' ), esc_html( $added_text ) );
}
it will look like this http://prntscr.com/f3hpsx
You can do that via wc_add_to_cart_message filter from woocommerce.
add_filter( 'wc_add_to_cart_message', 'adding_image_to_cart_message' );
function adding_image_to_cart_message() {
global $woocommerce;
$return_to = get_permalink(woocommerce_get_page_id('shop'));
$imgpath = 'add your image path here';
$message = sprintf('<img src="%s"/>%s %s', $imgpath , $return_to, __('Continue Shopping', 'woocommerce'), __('has been added to your cart.', 'woocommerce') );
return $message;
}
Add your image path to $imgpath variable.
I used the add to cart message hook in Woocommerce to edit the text and remove some classes from certain buttons. It seems this hook is now deprecated in Woocommerce 2.1 and I can't find an alternative.
I want to remove the 'button' class from the 'Continue Shopping' button. This class gets defined in the Woocommerce core which I want to leave unedited for proper future updates.
The line I'm trying to edit is located in woocommerce/includes/wc-cart-functions.php line 94.
$message = sprintf('%s %s', $return_to, __( 'Continue Shopping', 'woocommerce' ), $added_text );
Did anyone find a proper alternative for this hook yet? Thanks in advance!
This worked for me
add_filter( 'wc_add_to_cart_message', 'custom_add_to_cart_message' );
function custom_add_to_cart_message() {
global $woocommerce;
$return_to = get_permalink(woocommerce_get_page_id('shop'));
$message = sprintf('%s %s', $return_to, __('Continue Shopping', 'woocommerce'), __('Product successfully added to your cart.', 'woocommerce') );
return $message;
}
Edited: Thanks for the correction Kaarel Kaspar
Woocommerce 2.3+,
add_filter( 'wc_add_to_cart_message', 'custom_add_to_cart_message' );
function custom_add_to_cart_message( $message ){
global $woocommerce;
$added_text = __( 'Product was successfully added to your Network Kit.', 'woocommerce' );
// 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 %s', $return_to, __( 'Continue Shopping', 'woocommerce' ), $added_text );
else :
$message = sprintf('%s %s', wc_get_page_permalink( 'cart' ), __( 'View your Network Kit', 'woocommerce' ), $added_text );
endif;
return $message;
}
Although this thread is a bit old, I found the link to my same question about deprecated since version 3.0! from the first link on G search engine, so here is what fixed my errors.
Errors:
Notice: The wc_add_to_cart_message filter is deprecated since version 3.0! Use wc_add_to_cart_message_html instead. in /sitepath.com/wp-includes/functions.php on line 4329
Notice: woocommerce_get_page_id is deprecated since version 3.0! Use wc_get_page_id instead. in /sitepath.com/wp-includes/functions.php on line 4329
As you can see the solution is in the problem (error message).
Use wc_add_to_cart_message_html
Use wc_get_page_id instead
As of 2020 this is the filter required: wc_add_to_cart_message has been deprecated. Here I am simply wrapping the message in span:
//New added to card message text
function filter_wc_add_to_cart_message_html( $message, $products ) {
return "<span>".$message."</span>";
};
add_filter( 'wc_add_to_cart_message_html', 'filter_wc_add_to_cart_message_html', 10, 2 );
this could be a solution. please change if you have better ways or ideas:
the filter-name has changed with the 2.1-version to "wc_add_to_cart_message"
add_filter( 'wc_add_to_cart_message', 'foo' );
function foo() {
$product_id = $_REQUEST[ 'product_id' ];
if ( is_array( $product_id ) ) {
$titles = array();
foreach ( $product_id as $id ) {
$titles[] = get_the_title( $id );
}
$added_text = sprintf( __( 'Added "%s" to your cart.', 'woocommerce' ), join( __( '" and "', 'woocommerce' ), array_filter( array_merge( array( join( '", "', array_slice( $titles, 0, -1 ) ) ), array_slice( $titles, -1 ) ) ) ) );
} else {
$added_text = sprintf( __( '"%s" was successfully added to your cart.', 'woocommerce' ), get_the_title( $product_id ) );
}
// 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 → %s',
$return_to, __( 'Continue Shopping', 'woocommerce' ),
$added_text
);
else :
$message = sprintf(
'%s → %s',
get_permalink( wc_get_page_id( 'cart' ) ),
__( 'View Cart', 'woocommerce' ),
$added_text );
endif;
return $message;
}
hope it helps.
cheers