custom/plugins/GrimmTheme/src/Storefront/Subscriber/ProductOptionDetailSubscriber.php line 43

Open in your IDE?
  1. <?php
  2. namespace GrimmTheme\Storefront\Subscriber;
  3. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  4. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  5. use Shopware\Storefront\Page\Product\Configurator\ProductCombinationFinder;
  6. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class ProductOptionDetailSubscriber implements EventSubscriberInterface
  9. {
  10.     private static array $mimeTypeFilter = [
  11.         'application/pdf'
  12.     ];
  13.     private SalesChannelRepositoryInterface $productRepository;
  14.     private ProductCombinationFinder $productCombinationFinder;
  15.     public function __construct(
  16.         SalesChannelRepositoryInterface $productRepository,
  17.         ProductCombinationFinder        $productCombinationFinder
  18.     )
  19.     {
  20.         $this->productRepository $productRepository;
  21.         $this->productCombinationFinder $productCombinationFinder;
  22.     }
  23.     public static function getSubscribedEvents(): array
  24.     {
  25.         return [
  26.             ProductPageLoadedEvent::class => 'onProductDetailPage'
  27.         ];
  28.     }
  29.     /**
  30.      * Instead of using special images for options we are using product cover of the variant article
  31.      *
  32.      * @param ProductPageLoadedEvent $productPageLoadedEvent
  33.      */
  34.     public function onProductDetailPage(ProductPageLoadedEvent $productPageLoadedEvent)
  35.     {
  36.         $elements $productPageLoadedEvent->getPage()->getConfiguratorSettings()->getElements();
  37.         foreach ($elements as $element) {
  38.             if ($element->getDisplayType() === 'media') {
  39.                 foreach ($element->getOptions() as $option) {
  40.                     $setting $option->getConfiguratorSetting();
  41.                     $combination $this->productCombinationFinder->find(
  42.                         $setting->getProductId(),
  43.                         $option->getGroupId(),
  44.                         [$setting->getOptionId()],
  45.                         $productPageLoadedEvent->getSalesChannelContext()
  46.                     );
  47.                     $variantId $combination->getVariantId();
  48.                     if ($variantId) {
  49.                         /** @var \Shopware\Core\Content\Product\SalesChannel\SalesChannelProductEntity $variant */
  50.                         $variant $this->productRepository->search(new Criteria([$variantId]), $productPageLoadedEvent->getSalesChannelContext())->first();
  51.                         if ($variant) {
  52.                             $cover $variant->getCoverId();
  53.                             if ($cover) {
  54.                                 $option->setMediaId($cover);
  55.                                 $option->setMedia($variant->getCover()->getMedia());
  56.                                 $setting->setMediaId($cover);
  57.                                 $setting->setMedia($variant->getCover()->getMedia());
  58.                             }
  59.                         }
  60.                     }
  61.                 }
  62.             }
  63.         }
  64.     }
  65. }