src/Storefront/Event/CartMergedSubscriber.php line 31

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Storefront\Event;
  3. use Shopware\Core\Checkout\Cart\Event\CartMergedEvent;
  4. use Shopware\Core\Framework\Log\Package;
  5. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  6. use Symfony\Component\HttpFoundation\RequestStack;
  7. use Symfony\Contracts\Translation\TranslatorInterface;
  8. /**
  9.  * @internal
  10.  */
  11. #[Package('storefront')]
  12. class CartMergedSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @internal
  16.      */
  17.     public function __construct(private readonly TranslatorInterface $translator, private readonly RequestStack $requestStack)
  18.     {
  19.     }
  20.     public static function getSubscribedEvents(): array
  21.     {
  22.         return [
  23.             CartMergedEvent::class => 'addCartMergedNoticeFlash',
  24.         ];
  25.     }
  26.     public function addCartMergedNoticeFlash(CartMergedEvent $event): void
  27.     {
  28.         $mainRequest $this->requestStack->getMainRequest();
  29.         if ($mainRequest === null) {
  30.             return;
  31.         }
  32.         if ($mainRequest->hasSession() === false) {
  33.             return;
  34.         }
  35.         $session $mainRequest->getSession();
  36.         if (!method_exists($session'getFlashBag')) {
  37.             return;
  38.         }
  39.         $session->getFlashBag()->add('info'$this->translator->trans('checkout.cart-merged-hint'));
  40.     }
  41. }