Suppress the "<product> added to your cart" message on redirect?

Created on 11 June 2021, over 3 years ago
Updated 23 May 2023, over 1 year ago

For sites that provide only products that are redirected to the checkout route via this module, the Commerce Cart message that "
added to your cart" can feel a bit unnecessary. There doesn't seem to be an option or easy way to hide just that message (due to Drupal's/Symfony's messaging APIs), other than to unregister the Commerce Cart event subscriber that generates it, which works for now but could break in the future if they add more functionality to that even subscriber. Anyways, here's my hack for now in case anyone else needs a quick solution. Don't forget to replace YOUR_MODULE with the machine name of your module.

YOUR_MODULE.services.yml:

  # Event subscriber to remove the Commerce added to cart message.
  YOUR_MODULE.kernel_commerce_cart_added_message_removal_event_subscriber:
    class: Drupal\YOUR_MODULE\EventSubscriber\Kernel\CommerceCartAddedMessageRemovalEventSubscriber
    arguments:
      - '@commerce_cart.cart_subscriber'
    tags:
      - { name: 'event_subscriber' }

YOUR_MODULE/src/EventSubscriber/Kernel/CommerceCartAddedMessageRemovalEventSubscriber.php:

<?php

namespace Drupal\YOUR_MODULE\EventSubscriber\Kernel;

use Drupal\commerce_cart\EventSubscriber\CartEventSubscriber;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
 * Event subscriber to remove the Commerce added to cart message.
 *
 * Since there doesn't seem to be a safe and easy way to remove a single message
 * from Drupal's messenger service, we instead remove the event subscriber that
 * produces it, as that's the only function of that event subscriber at the time
 * of writing. This may require revisiting if the Commerce Cart module later
 * adds more functionality to that event subscriber, which could break upon
 * being removed.
 *
 * @todo Extend the Commerce Cart event subscriber to conditionally add the
 *   message if a redirect was not performed?
 *
 * @see \Drupal\commerce_cart\EventSubscriber\CartEventSubscriber
 *   The Commerce Cart event subscriber that we remove.
 *
 * @see \Drupal\commerce_cart_redirection\EventSubscriber\CommerceCartRedirectionSubscriber
 *   Commerce Cart Redirection event subscriber that redirects to the checkout
 *   route if a product bundle is configured to do so.
 */
class CommerceCartAddedMessageRemovalEventSubscriber implements EventSubscriberInterface {

  /**
   * The Commerce Cart event subscriber that we remove.
   *
   * @var \Drupal\commerce_cart\EventSubscriber\CartEventSubscriber
   */
  protected $cartEventSubscriber;

  /**
   * Event subscriber constructor; saves dependencies.
   *
   * @param \Drupal\commerce_cart\EventSubscriber\CartEventSubscriber $cartEventSubscriber
   *   The Commerce Cart event subscriber that we remove.
   */
  public function __construct(CartEventSubscriber $cartEventSubscriber) {
    $this->cartEventSubscriber = $cartEventSubscriber;
  }

  /**
   * {@inheritdoc}
   */
  public static function getSubscribedEvents(): array {
    return [
      KernelEvents::REQUEST => 'onKernelRequest',
    ];
  }

  /**
   * Removes the event subscriber that produces the added to cart message.
   *
   * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event
   *   Symfony response event object.
   *
   * @param string $eventName
   *   The name of the event being dispatched.
   *
   * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $eventDispatcher
   *   The Symfony event dispatcher service.
   */
  public function onKernelRequest(
    GetResponseEvent $event,
    string $eventName,
    EventDispatcherInterface $eventDispatcher
  ): void {
    $eventDispatcher->removeSubscriber($this->cartEventSubscriber);
  }

}
โœจ Feature request
Status

Closed: won't fix

Version

3.2

Component

Code

Created by

๐Ÿ‡จ๐Ÿ‡ฆCanada ambient.impact Toronto

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany rgpublic Dรผsseldorf ๐Ÿ‡ฉ๐Ÿ‡ช ๐Ÿ‡ช๐Ÿ‡บ

    I still think this is badly needed. It's not really a great solution to just hide the message via CSS as this will suppress ANY (probably important) error message. There's not an easy other way either to get rid of this message for template/theme authors. And I don't see any likelihood to have this solved in Commerce Core in the forseeable future.

    I've attached a patch who tries to solve this a little less aggressively than what was originally propose in the issue description. I tried to use best practices to make this as little intrusive as possible. I'm using a service decorator and I'm only decorating the problematic function that really generates the unwanted message in the first place. If in the future the Drupal Commerce team includes any other important functionality in the Event Subscriber that won't get thrown out.

    In addition, I've made this behavior completely configurable, of course. What are y'all thinking about this? Good idea? Safe to go in?

  • Status changed to Postponed almost 2 years ago
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand andyd328 Lyttelton, NZ ๐Ÿ‡ณ๐Ÿ‡ฟ

    Hi rgpublic,

    Thanks for the patch and thoughts, I agree that suppressing the add to cart message is needed, I've been hoping that it will get into Commerce core and not need anything here.

    Have you seen https://www.drupal.org/project/commerce/issues/3317738 โœจ Add an option to disable the add to cart message on a certain order type Fixed what do you think about trying to see if that can get it into commerce core? It looks like a nice discrete change to Commerce could solve this use case.

    Kind regards,

    Andy

  • ๐Ÿ‡ฉ๐Ÿ‡ชGermany rgpublic Dรผsseldorf ๐Ÿ‡ฉ๐Ÿ‡ช ๐Ÿ‡ช๐Ÿ‡บ

    @AndyD328: Got you. I've subscribed and commented over there. Thanks for the pointer.

  • Status changed to Closed: won't fix over 1 year ago
  • ๐Ÿ‡ณ๐Ÿ‡ฟNew Zealand andyd328 Lyttelton, NZ ๐Ÿ‡ณ๐Ÿ‡ฟ

    Closing this as 3317738 has now been committed to commerce core. Thanks everyone!

Production build 0.71.5 2024