custom/plugins/ShopStudioPixelYourShop/src/EventSubscriber/CheckoutCartEventSubscriber.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ShopStudio\PixelYourShop\EventSubscriber;
  3. use ShopStudio\PixelYourShop\View\TransientBag\Transient;
  4. use ShopStudio\PixelYourShop\View\TransientBag\TransientBagInterface;
  5. use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
  6. use Shopware\Core\Checkout\Cart\Event\AfterLineItemRemovedEvent;
  7. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. /**
  10.  * @since 1.0.0
  11.  */
  12. class CheckoutCartEventSubscriber implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @since 1.0.0
  16.      */
  17.     protected TransientBagInterface $transientBag;
  18.     /**
  19.      * @since 2.0.0
  20.      */
  21.     public function __construct(TransientBagInterface $transientBag)
  22.     {
  23.         $this->transientBag $transientBag;
  24.     }
  25.     /**
  26.      * @since 1.0.0
  27.      *
  28.      * @inheritDoc
  29.      */
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             AfterLineItemAddedEvent::class => 'onAfterLineItemAddedEvent',
  34.             AfterLineItemRemovedEvent::class => 'onAfterLineItemRemovedEvent',
  35.         ];
  36.     }
  37.     /**
  38.      * @since 2.0.0
  39.      */
  40.     public function onAfterLineItemAddedEvent(AfterLineItemAddedEvent $event): void
  41.     {
  42.         $lineItem $event->getLineItems()[0] ?? null;
  43.         if ($lineItem !== null && $lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
  44.             $this->transientBag->set(new Transient('add_to_cart'$lineItem));
  45.         }
  46.     }
  47.     /**
  48.      * @since 2.6.1
  49.      */
  50.     public function onAfterLineItemRemovedEvent(AfterLineItemRemovedEvent $event): void
  51.     {
  52.         $lineItem $event->getLineItems()[0] ?? null;
  53.         if ($lineItem !== null && $lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
  54.             $this->transientBag->set(new Transient('remove_from_cart'$lineItem));
  55.         }
  56.     }
  57. }