Skip to main content
2 of 6
added 694 characters in body

Alter add to cart message

How to alter the message shown ("xyz" added to your cart) when we add an item to cart in Drupal 8 commerce 2x.

I saw Drupal\commerce_cart\EventSubscriber where the function displayAddToCartMessage(CartEntityAddEvent $event) is written which is responsible for showing the message, is there any method to override it.

I Found the solution but i think there has to be a better method than this.I wrote a custom submit handler on Add to Cart Submit => $form['actions']['submit']['#submit'][] = 'example';

and then in

function example(array $form, FormStateInterface $form_state) {

// TO Unset The message which is shown add an item is added to cart.

unset($_SESSION['_symfony_flashes']['status']);

// Path To Cart.    

$link = \Drupal\Core\Url::fromRoute('commerce_cart.page')->toString();
// $get_cart_title is some variable as per requirement.

// Your custom message. 

drupal_set_message(t($get_cart_title.' added to your cart', array('@link' => $link)));

}