custom/plugins/UandiEfbConfigurableProduct/src/Subscriber/CheckoutPageSubscriber.php line 56

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Uandi\UandiEfbConfigurableProduct\Subscriber;
  3. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItemCollection;
  5. use Shopware\Core\Checkout\Order\Aggregate\OrderLineItem\OrderLineItemEntity;
  6. use Shopware\Core\Framework\Context;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\Struct\ArrayStruct;
  10. use Shopware\Core\Framework\Struct\Collection;
  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\Finish\CheckoutFinishPageLoadedEvent;
  15. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  16. use Shopware\Storefront\Page\Page;
  17. use Shopware\Storefront\Page\PageLoadedEvent;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Uandi\UandiEfbConfigurableProduct\Service\ConfigurableHandler;
  20. use Uandi\UandiEfbConfigurableProduct\Service\ConfiguratorToken;
  21. class CheckoutPageSubscriber implements EventSubscriberInterface
  22. {
  23.     private const CONFIG_KEYS = [
  24.         'default' => 'UandiEfbConfigurableProduct.config.defaultImage',
  25.         'lwl' => 'UandiEfbConfigurableProduct.config.lwlImage',
  26.         'cabinet' => 'UandiEfbConfigurableProduct.config.cabinetImage',
  27.     ];
  28.     private SystemConfigService $systemConfigService;
  29.     private EntityRepositoryInterface $mediaRepository;
  30.     private ConfiguratorToken $configuratorToken;
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return array_fill_keys([
  34.             CheckoutCartPageLoadedEvent::class,
  35.             CheckoutConfirmPageLoadedEvent::class,
  36.             CheckoutFinishPageLoadedEvent::class,
  37.             OffcanvasCartPageLoadedEvent::class,
  38.         ], 'onAnyCartPageLoaded');
  39.     }
  40.     public function __construct(
  41.         SystemConfigService $systemConfigService,
  42.         EntityRepositoryInterface $mediaRepository,
  43.         ConfiguratorToken $configuratorToken
  44.     ) {
  45.         $this->systemConfigService $systemConfigService;
  46.         $this->mediaRepository $mediaRepository;
  47.         $this->configuratorToken $configuratorToken;
  48.     }
  49.     public function onAnyCartPageLoaded(PageLoadedEvent $event)
  50.     {
  51.         $page $event->getPage();
  52.         $context $event->getSalesChannelContext();
  53.         $lineItems $this->extractLineItems($page);
  54.         foreach ($lineItems as $lineItem) {
  55.             if ($this->isConfigurableProduct($lineItem)) {
  56.                 $this->loadImages($page$event->getContext());
  57.                 break;
  58.             }
  59.         }
  60.         if ($context->getCustomer()) {
  61.             $token $this->configuratorToken->generateTokenByCustomer($context->getCustomer(), $context->getToken());
  62.             $page->addExtension('configuratorToken', new ArrayStruct(['token' => rawurlencode($token)]));
  63.         }
  64.     }
  65.     private function loadImages(Page $pageContext $context): void
  66.     {
  67.         $mediaIds array_filter(array_map([$this->systemConfigService'get'], static::CONFIG_KEYS));
  68.         $media $this->mediaRepository->search(new Criteria($mediaIds), $context);
  69.         if ($media->count() <= 0) {
  70.             return;
  71.         }
  72.         $media array_map([$media'get'], $mediaIds);
  73.         $page->addExtension('configurableProductImages', new ArrayStruct($media));
  74.     }
  75.     private function isConfigurableProduct($lineItem): bool
  76.     {
  77.         $isLineItem $lineItem instanceof LineItem || $lineItem instanceof OrderLineItemEntity;
  78.         return $isLineItem && $lineItem->getType() === ConfigurableHandler::TYPE;
  79.     }
  80.     private function extractLineItems(Page $page): Collection
  81.     {
  82.         if (method_exists($page'getCart')) {
  83.             return $page->getCart()->getLineItems();
  84.         }
  85.         if (method_exists($page'getOrder')) {
  86.             return $page->getOrder()->getLineItems();
  87.         }
  88.         return new LineItemCollection();
  89.     }
  90. }