custom/plugins/SasVariantSwitch/src/Subscriber/CartPageLoadedSubscriber.php line 87

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace SasVariantSwitch\Subscriber;
  3. use SasVariantSwitch\SasVariantSwitch;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  5. use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
  6. use Shopware\Core\Content\Product\ProductCollection;
  7. use Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  10. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  11. use Shopware\Core\System\SystemConfig\SystemConfigService;
  12. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  13. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  14. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. use SasVariantSwitch\Storefront\Page\ProductListingConfigurationLoader;
  17. class CartPageLoadedSubscriber implements EventSubscriberInterface
  18. {
  19.     private ProductListingConfigurationLoader $listingConfigurationLoader;
  20.     private SalesChannelRepositoryInterface $productRepository;
  21.     private SystemConfigService $systemConfigService;
  22.     public function __construct(
  23.         SalesChannelRepositoryInterface $productRepository,
  24.         ProductListingConfigurationLoader $listingConfigurationLoader,
  25.         SystemConfigService $systemConfigService
  26.     ) {
  27.         $this->listingConfigurationLoader $listingConfigurationLoader;
  28.         $this->productRepository $productRepository;
  29.         $this->systemConfigService $systemConfigService;
  30.     }
  31.     public static function getSubscribedEvents()
  32.     {
  33.         return [
  34.             OffcanvasCartPageLoadedEvent::class => [
  35.                 ['onOffCanvasCartPageLoaded'201],
  36.             ],
  37.             CheckoutCartPageLoadedEvent::class => [
  38.                 ['onCheckoutCartPageLoaded'201],
  39.             ],
  40.             CheckoutConfirmPageLoadedEvent::class => [
  41.                 ['onCheckoutConfirmPageLoaded'201],
  42.             ]
  43.         ];
  44.     }
  45.     public function onOffCanvasCartPageLoaded(OffcanvasCartPageLoadedEvent $event): void
  46.     {
  47.         $context $event->getSalesChannelContext();
  48.         if (!$this->systemConfigService->getBool(SasVariantSwitch::SHOW_ON_OFFCANVAS_CART$context->getSalesChannelId())) {
  49.             return;
  50.         }
  51.         $lineItems $event->getPage()->getCart()->getLineItems()->filterType(LineItem::PRODUCT_LINE_ITEM_TYPE);
  52.         if ($lineItems->count() === 0) {
  53.             return;
  54.         }
  55.         $context $event->getSalesChannelContext();
  56.         $this->addLineItemPropertyGroups($lineItems$context);
  57.     }
  58.     public function onCheckoutCartPageLoaded(CheckoutCartPageLoadedEvent $event): void
  59.     {
  60.         $context $event->getSalesChannelContext();
  61.         if (!$this->systemConfigService->getBool(SasVariantSwitch::SHOW_ON_CART_PAGE$context->getSalesChannelId())) {
  62.             return;
  63.         }
  64.         $lineItems $event->getPage()->getCart()->getLineItems()->filterType(LineItem::PRODUCT_LINE_ITEM_TYPE);
  65.         if ($lineItems->count() === 0) {
  66.             return;
  67.         }
  68.         $this->addLineItemPropertyGroups($lineItems$context);
  69.     }
  70.     public function onCheckoutConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
  71.     {
  72.         $context $event->getSalesChannelContext();
  73.         if (!$this->systemConfigService->getBool(SasVariantSwitch::SHOW_ON_CHECKOUT_CONFIRM_PAGE$context->getSalesChannelId())) {
  74.             return;
  75.         }
  76.         $lineItems $event->getPage()->getCart()->getLineItems()->filterType(LineItem::PRODUCT_LINE_ITEM_TYPE);
  77.         if ($lineItems->count() === 0) {
  78.             return;
  79.         }
  80.         $this->addLineItemPropertyGroups($lineItems$context);
  81.     }
  82.     private function addLineItemPropertyGroups(LineItemCollection $lineItemsSalesChannelContext $context): void
  83.     {
  84.         $productIds $lineItems->getReferenceIds();
  85.         $criteria = new Criteria($productIds);
  86.         /** @var ProductCollection $products */
  87.         $products $this->productRepository->search($criteria$context)->getEntities();
  88.         if ($products->count() <= 0) {
  89.             return;
  90.         }
  91.         $this->listingConfigurationLoader->loadListing($products$context);
  92.         /** @var SalesChannelProductEntity $product */
  93.         foreach ($products as $product) {
  94.             if ($product->getExtension('groups') !== null) {
  95.                 $lineItem $lineItems->get($product->getId());
  96.                 $lineItem->addExtension('groups'$product->getExtension('groups'));
  97.                 $lineItem->setPayloadValue('parentId'$product->getParentId());
  98.                 $lineItem->setPayloadValue('optionIds'$product->getOptionIds());
  99.             }
  100.         }
  101.     }
  102. }