vendor/store.shopware.com/proxabasketcrosssellingsw6/src/Subscriber/Storefront/EventSubscriber.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace ProxaBasketCrossSellingSW6\Subscriber\Storefront;
  3. use Shopware\Core\Framework\Struct\ArrayEntity;
  4. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  5. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  6. use Shopware\Storefront\Page\PageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. use ProxaBasketCrossSellingSW6\Service\GetSliderProductsService;
  9. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  10. class EventSubscriber implements EventSubscriberInterface
  11. {
  12.     /**
  13.      * @var GetSliderProductsService
  14.      */
  15.     private $getSliderProductsService;
  16.     /**
  17.      * EventSubscriber constructor.
  18.      * @param GetSliderProductsService $getSliderProductsService
  19.      */
  20.     public function __construct(GetSliderProductsService $getSliderProductsService)
  21.     {
  22.         $this->getSliderProductsService $getSliderProductsService;
  23.     }
  24.     /**
  25.      * @return array|string[]
  26.      */
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             CheckoutCartPageLoadedEvent::class => 'onCartEvent',
  31.             OffcanvasCartPageLoadedEvent::class => 'onCartEvent'
  32.         ];
  33.     }
  34.     /**
  35.      * @param PageLoadedEvent $event
  36.      * @return void
  37.      * @throws \Doctrine\DBAL\Driver\Exception
  38.      * @throws \Doctrine\DBAL\Exception
  39.      */
  40.     public function onCartEvent(PageLoadedEvent $event)
  41.     {
  42.         if ($event instanceof CheckoutCartPageLoadedEvent || $event instanceof OffcanvasCartPageLoadedEvent) {
  43.             $cart $event->getPage()->getCart();
  44.             $cartItems $cart->getLineItems();
  45.             $salesChannelContext $event->getSalesChannelContext();
  46.             if ($event instanceof OffcanvasCartPageLoadedEvent) {
  47.                 $viewType 'AjaxCart';
  48.             } else {
  49.                 $viewType 'Basket';
  50.             }
  51.             $cartItemNumbers = [];
  52.             foreach ($cartItems as $cartItem) {
  53.                 if ($cartItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
  54.                     $cartItemNumbers[] = $cartItem->getPayload()['productNumber'];
  55.                 }
  56.             }
  57.             $this->getSliderProductsService->setViewType($viewType);
  58.             $this->getSliderProductsService->setContext($salesChannelContext);
  59.             $cartItemNumbers array_reverse($cartItemNumbers);
  60.             $this->getSliderProductsService->setCartItemNumbers($cartItemNumbers);
  61.             $sliderProducts $this->getSliderProductsService->getSliderProducts($salesChannelContext->getSalesChannelId());
  62.             $sliderDisplayingType $this->getSliderProductsService->getSliderTypeInAjaxCart();
  63.             $cart->addExtensions(['sliderProducts' => new ArrayEntity($sliderProducts), 'sliderType' => new ArrayEntity([$sliderDisplayingType])]);
  64.         }
  65.     }
  66. }