Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

WooCommerce Added to Cart message WordPress

  • SOLVED

In the plugin file woocommerce-functions.php are several references to the function 'woocommerce_add_to_cart_message'. And, in the folder 'templates/shop' is a file called messages.php that contains the code block:


<?php foreach ( $messages as $message ) : ?>
<div class="woocommerce-message"><?php echo wp_kses_post( $message ); ?></div>
<?php endforeach; ?>


Please can someone help me write a new function that outputs "Item successfully added to cart" after a product has indeed been successfully added to the cart. This message should display just below the 'Add to Cart' button in grid view and on individual product pages.

This message should compliment, not replace the default massage that is displayed at the top of a page after a product has been successfully added to the cart.

Thank you.

Answers (2)

2013-04-24

Yakir Sitbon answers:

In functions.php:
function ys_wc_custom_add_to_cart_message( $massage ) {
$_SESSION['ys-atcm'] = 'Item successfully added to cart';

return $massage;
}
add_filter( 'woocommerce_add_to_cart_message', 'ys_wc_custom_add_to_cart_message' );


And in view tpl:
if ( ! empty( $_SESSION['ys-atcm'] ) ) :
echo $_SESSION['ys-atcm'];
unset( $_SESSION['ys-atcm'] );
endif;


Good lucky ;)


designbuildtest comments:

Sorry Yakir, doesn't seem to work.


Yakir Sitbon comments:

Are you understanding what I code for you?


designbuildtest comments:

I think so. Have you tested on your own site?


Yakir Sitbon comments:

Can you show me what you do? It's need to work man..


designbuildtest comments:

Hi Yakir, I put this in functions.php...


function ys_wc_custom_add_to_cart_message( $message ) {
$_SESSION['ys-atcm'] = 'Item successfully added to cart';
return $message;
}
add_filter( 'woocommerce_add_to_cart_message', 'ys_wc_custom_add_to_cart_message' );


and this in content-product.php


if ( ! empty( $_SESSION['ys-atcm'] ) ) :
echo $_SESSION['ys-atcm'];
unset( $_SESSION['ys-atcm'] );
endif;


This adds/changes nothing unfortunately.

Just to be clear, I don't want to replace <strong>woocommerce_add_to_cart_message()</strong>, rather I need a new function that performs similarly, but outputs the message 'Item successfully added to cart' below the 'Add to Cart' button on product pages (list/grid and single view).


Yakir Sitbon comments:

Your session not started.. Just add this code in functions.php

function ys_init_session_start() {
// make sure session ready for work..
if ( ! session_id() )
session_start();
}
add_action( 'init', 'ys_init_session_start' );


designbuildtest comments:

Hi Yakir, added the above to functions.php, but no change. To reiterate, I do not want to modify the existing function <strong>woocommerce_add_to_cart_message()</strong> (isn't this what you proposed solution attempts to do by adding a filter???)


Yakir Sitbon comments:

I will check this later.. But what I do here.. I save the message in session, and return original message.


Yakir Sitbon comments:

OK, sorry about my first code, now I test my code and its proper one:

Please remove all my codes and add this:

In functions.php file put:
function ys_wc_custom_add_to_cart_message( $message ) {
global $woocommerce;
$woocommerce->session->ys_atcm = 'Item successfully added to cart';

return $message;
}
add_filter( 'woocommerce_add_to_cart_message', 'ys_wc_custom_add_to_cart_message' );

function ys_print_custom_add_to_cart_message() {
global $woocommerce;

if ( ! empty( $woocommerce->session->ys_atcm ) ) {
echo $woocommerce->session->ys_atcm;
unset( $woocommerce->session->ys_atcm );
}
}


In your view use with:
ys_print_custom_add_to_cart_message()

Good luck. :)
Yakir.


designbuildtest comments:

Hi Yakir, thanks for this. Your suggested solution does 50% of what I need. It works on a single product page, but not within the products loop. The linked text 'View Cart &rarr' is currently rendered beneath the 'Add to Cart' button when a product is added to the cart from within looped pages (products, categories, search etc). This text is added to the page via AJAX and can be seen in the file woocommerce.php on line 1210. Can you please have a look to see if this text is pluggable?
Thank you.


Yakir Sitbon comments:

Hmm.. If you want add this text by AJAX, you can create event by jQuery and add this text.
But if you want add this text in multiple area, I can change this function with param if erase the session or not.

Yakir.


designbuildtest comments:

Thanks Yakir, any further help is very much appreciated. Making thinks a little more difficult is the fact I am using a gist from Mike Jolley - [[LINK href="https://gist.github.com/mikejolley/2793710"]]https://gist.github.com/mikejolley/2793710[[/LINK]] - to render product quantities within the loop. When a product is added to the cart from a looped page, a confirmation message of 'Added to Cart' should appear below the relevant product. Thanks again.

2013-04-24

Daniel Yoen answers:

try this :

function my_cart_messages($message)
{
$newmessage = 'Item successfully added to cart';
$replacemessage = '<a$1class="button">' . $newmessage .'</a>';
$message = preg_replace('#<a(.*?)class="button">(.*?)</a>#', $replacemessage, $message);

return $message;
}
add_filter( 'woocommerce_add_to_cart_message', 'my_cart_messages', 99);


hope this help :-)


designbuildtest comments:

Hi Daniel, please have another read of my question. Your solution replaces the default 'Add to Cart' message. I require a complete new function that compliments rather than changes the existing function.