custom/plugins/EconsorBundles/src/Subscriber/BundleSubscriber.php line 89

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace EconsorBundles\Subscriber;
  3. use EconsorBundles\Services\BundleService;
  4. use EconsorBundles\Subscriber\Struct\ColorStruct;
  5. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  6. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  7. use Shopware\Core\Checkout\Customer\Event\CustomerLoginEvent;
  8. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  10. use Shopware\Core\Framework\Struct\Struct;
  11. use Shopware\Core\System\System;
  12. use Shopware\Core\System\SystemConfig\SystemConfigService;
  13. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  14. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  17. class BundleSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var CartService
  21.      */
  22.     private $cartService;
  23.     /**
  24.      * @var BundleService
  25.      */
  26.     private $bundleService;
  27.     /**
  28.      * @var SessionInterface
  29.      */
  30.     private $session;
  31.     /**
  32.      * @var EntityRepositoryInterface
  33.      */
  34.     private $ecBundlesRepository;
  35.     /**
  36.      * @var EntityRepositoryInterface
  37.      */
  38.     private $propertyGroupRepository;
  39.     /**
  40.      * @var SystemConfigService
  41.      */
  42.     private $systemConfigService;
  43.     public function __construct(CartService $cartServiceBundleService $bundleServiceSessionInterface $sessionEntityRepositoryInterface $ecBundlesRepositoryEntityRepositoryInterface $propertyGroupRepositorySystemConfigService $systemConfigService)
  44.     {
  45.         $this->cartService $cartService;
  46.         $this->bundleService $bundleService;
  47.         $this->session $session;
  48.         $this->ecBundlesRepository $ecBundlesRepository;
  49.         $this->propertyGroupRepository $propertyGroupRepository;
  50.         $this->systemConfigService $systemConfigService;
  51.     }
  52.     public static function getSubscribedEvents(): array
  53.     {
  54.         return [
  55.             BeforeLineItemAddedEvent::class => 'denyQuantityChange',
  56.             CustomerLoginEvent::class => 'changeSessionId',
  57.             OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCartLoaded',
  58.             CheckoutCartPageLoadedEvent::class => 'onCheckoutCartPageLoaded'
  59.         ];
  60.     }
  61.     public function denyQuantityChange(BeforeLineItemAddedEvent $event)
  62.     {
  63.         if ('econsorbundle' === $event->getLineItem()->getType()) {
  64.             $bundleLineItems $event->getCart()->getLineItems()->filterType('econsorbundle');
  65.             foreach ($bundleLineItems as $bundleLineItem) {
  66.                 if ($bundleLineItem->getReferencedId() === $event->getLineItem()->getReferencedId()) {
  67.                     $event->getCart()->getLineItems()->get($bundleLineItem->getReferencedId())->setQuantity(1);
  68.                 } else {
  69.                     $event->getCart()->getLineItems()->removeElement($bundleLineItem);
  70.                 }
  71.             }
  72.         }
  73.     }
  74.     public function changeSessionId(CustomerLoginEvent $event) :void
  75.     {
  76.         $bundleLineItems $this->cartService->getCart(
  77.             $event->getContextToken(),
  78.             $event->getSalesChannelContext()
  79.         )->getLineItems()->filterType('econsorbundle');
  80.         foreach ($bundleLineItems as $bundleLineItem) {
  81.             if ($this->bundleService->hasKey($event->getSalesChannelContext(), $bundleLineItem->getId())) {
  82.                 $this->ecBundlesRepository->update(
  83.                     [
  84.                         [
  85.                             'id' => $bundleLineItem->getId(),
  86.                             'token' => $event->getContextToken(),
  87.                             'sessionId' => $this->session->getId()
  88.                         ]
  89.                     ],
  90.                     $event->getContext()
  91.                 );
  92.             }
  93.         }
  94.     }
  95.     public function onOffcanvasCartLoaded(OffcanvasCartPageLoadedEvent $event)
  96.     {
  97.         $this->getColorGroup($event);
  98.     }
  99.     public function onCheckoutCartPageLoaded(CheckoutCartPageLoadedEvent $event)
  100.     {
  101.         $this->getColorGroup($event);
  102.     }
  103.     public function getColorGroup($event)
  104.     {
  105.         $colorId $this->systemConfigService->get('EconsorBundles.config.ecBundlesColor');
  106.         $propertyGroup $this->propertyGroupRepository->search(new Criteria([$colorId]), $event->getContext())->getEntities();
  107.         $colorGroup $propertyGroup->get($colorId)->getTranslated()['name'];
  108.         $colorStruct = new ColorStruct();
  109.         $colorStruct->setColorGroup($colorGroup);
  110.         $event->getPage()->addExtension('colorId'$colorStruct);
  111.     }
  112. }