Ask your WordPress questions! Pay money and get answers fast! (more info)

WooCommerce Added to Cart message WordPress

  • SOLVED

I am currently turning WooCommerce into a quoting system and am looking to replace the word "cart" in the add to cart message and button text with "quote" Please see attached screenshot for more info.

I have found the following code snippets that might help you with your answer. The first function updates the add to cart message, the second updates the button text.

1. Function that updates the message but replaces the product name with "Product":

add_filter( 'woocommerce_add_to_cart_message', 'custom_add_to_cart_message' );
function custom_add_to_cart_message ($message) {
global $woocommerce;
// Output success messages
$custom_message = sprintf('%s %s', __('Product successfully added to your ', 'woocommerce'), get_permalink(woocommerce_get_page_id('cart')), __('Briefcase', 'woocommerce'));
global $is_cart_added;
$is_cart_added = 1;
return $custom_message;
}


2. Function that updates the button text:

function my_cart_messages($message)

{
$newmessage = 'View Quote';
$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);




The original code can be found in the woocommerce-functions.php file on line 471


Thanks in advance for you help.

Ryan

Answers (3)

2013-07-10

Giri answers:

Try this

function woocommerce_custom_add_to_cart_message( $product_id ) {
global $woocommerce;

if ( is_array( $product_id ) ) {

$titles = array();

foreach ( $product_id as $id ) {
$titles[] = get_the_title( $id );
}

$added_text = sprintf( __( 'Added &quot;%s&quot; to your quote.', 'woocommerce' ), join( __( '&quot; and &quot;', 'woocommerce' ), array_filter( array_merge( array( join( '&quot;, &quot;', array_slice( $titles, 0, -1 ) ) ), array_slice( $titles, -1 ) ) ) ) );

} else {
$added_text = sprintf( __( '&quot;%s&quot; was successfully added to your quote.', '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('<a href="%s" class="button">%s</a> %s', $return_to, __( 'Continue Shopping &rarr;', 'woocommerce' ), $added_text );

else :

$message = sprintf('<a href="%s" class="button">%s</a> %s', get_permalink( woocommerce_get_page_id( 'cart' ) ), __( 'View Quote &rarr;', 'woocommerce' ), $added_text );

endif;

$woocommerce->add_message( $message );
}
add_filter( 'woocommerce_add_to_cart_message', 'woocommerce_custom_add_to_cart_message', 99);


Ryan Waters comments:

Hi Giri,

Thanks for your suggestion. This almost worked. It seemed to add an extra info box and the product name was missing from the message. Please see attached screenshot and let me know your thoughts.

Thanks
Ryan