custom/plugins/TwigelSportPlugins/src/Subscriber/ProductPageSubscriber.php line 93

Open in your IDE?
  1. <?php
  2. namespace Twigel\SportPlugin\Subscriber;
  3. use Shopware\Core\Checkout\Promotion\PromotionEntity;
  4. use Shopware\Core\Content\ProductStream\ProductStreamEntity;
  5. use Shopware\Core\Content\ProductStream\Service\ProductStreamBuilderInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  9. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class ProductPageSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var EntityRepositoryInterface
  17.      */
  18.     private EntityRepositoryInterface $productStreamRepository;
  19.     /**
  20.      * @var SalesChannelRepositoryInterface
  21.      */
  22.     private SalesChannelRepositoryInterface $productRepository;
  23.     /**
  24.      * @var ProductStreamBuilderInterface
  25.      */
  26.     private ProductStreamBuilderInterface $productStreamBuilder;
  27.     /**
  28.      * @var SystemConfigService
  29.      */
  30.     private SystemConfigService $systemConfigService;
  31.     /**
  32.      * @var EntityRepositoryInterface
  33.      */
  34.     private EntityRepositoryInterface $promotionRepository;
  35.     /**
  36.      * @var SalesChannelRepositoryInterface
  37.      */
  38.     private SalesChannelRepositoryInterface $categoryRepository;
  39.     /**
  40.      * @var EntityRepositoryInterface $manufacturerRepository ;
  41.      */
  42.     private EntityRepositoryInterface $manufacturerRepository;
  43.     /**
  44.      * @var EntityRepositoryInterface $mediaRepository ;
  45.      */
  46.     private EntityRepositoryInterface $mediaRepository;
  47.     /**
  48.      * @param EntityRepositoryInterface $productStreamRepository
  49.      * @param SalesChannelRepositoryInterface $productRepository
  50.      * @param ProductStreamBuilderInterface $productStreamBuilder
  51.      * @param SystemConfigService $systemConfigService
  52.      * @param EntityRepositoryInterface $promotionRepository
  53.      * @param SalesChannelRepositoryInterface $categoryRepository
  54.      */
  55.     public function __construct(
  56.         EntityRepositoryInterface       $productStreamRepository,
  57.         SalesChannelRepositoryInterface $productRepository,
  58.         ProductStreamBuilderInterface   $productStreamBuilder,
  59.         SystemConfigService             $systemConfigService,
  60.         EntityRepositoryInterface       $promotionRepository,
  61.         SalesChannelRepositoryInterface $categoryRepository,
  62.         EntityRepositoryInterface       $manufacturerRepository,
  63.         EntityRepositoryInterface       $mediaRepository
  64.     )
  65.     {
  66.         $this->productStreamRepository $productStreamRepository;
  67.         $this->productRepository $productRepository;
  68.         $this->productStreamBuilder $productStreamBuilder;
  69.         $this->systemConfigService $systemConfigService;
  70.         $this->promotionRepository $promotionRepository;
  71.         $this->categoryRepository $categoryRepository;
  72.         $this->manufacturerRepository $manufacturerRepository;
  73.         $this->mediaRepository $mediaRepository;
  74.     }
  75.     public static function getSubscribedEvents()
  76.     {
  77.         return [
  78.             ProductPageLoadedEvent::class => 'onLoad'
  79.         ];
  80.     }
  81.     public function onLoad(ProductPageLoadedEvent $event)
  82.     {
  83.         $pluginConfig $this->systemConfigService->get('TwigelSportPlugin.config');
  84.         if (array_key_exists("twigel_product_instruction_pdf"$event->getPage()->getProduct()->getCustomFields())) {
  85.             $documentId $event->getPage()->getProduct()->getCustomFields()['twigel_product_instruction_pdf'];
  86.             $criteria = new Criteria([$documentId]);
  87.             $document $this->mediaRepository->search($criteria$event->getContext())->first();
  88.             if ($document) {
  89.                 $event->getPage()->assign(["pdf" => $document]);
  90.             }
  91.         }
  92.         if ($event->getPage()->getCrossSellings()) {
  93.             $crossSellings $event->getPage()->getCrossSellings()->getElements();
  94.             if (!empty($crossSellings)) {
  95.                 $crossSellings $event->getPage()->getCrossSellings()->getElements()[0];
  96.                 $products $crossSellings->getProducts();
  97.                 foreach ($products as $product) {
  98.                     $manufacturerId $product->getManufacturerId();
  99.                     $criteria = new Criteria([$manufacturerId]);
  100.                     $manufacturer $this->manufacturerRepository->search($criteria$event->getContext())->first();
  101.                     $product->setManufacturer($manufacturer);
  102.                 }
  103.             }
  104.         }
  105.         if (array_key_exists('setCategory'$pluginConfig) && $pluginConfig['setCategory']) {
  106.             $setCategory $pluginConfig['setCategory'];
  107.             if (in_array($setCategory$event->getPage()->getProduct()->getCategoryTree())) {
  108.                 $productCustomFields $event->getPage()->getProduct()->getCustomFields();
  109.                 if (array_key_exists('productSet'$event->getPage()->getProduct()->getCustomFields())) {
  110.                     $productSet $productCustomFields['productSet'];
  111.                     $productsSet $this->productRepository->search((new Criteria($productSet))->addAssociation('media'), $event->getSalesChannelContext())->getEntities();
  112.                     if (!empty($productSet)) {
  113.                         $event->getPage()->assign(['productSet' => $productsSet]);
  114.                     }
  115.                 }
  116.             }
  117.         }
  118.         if (array_key_exists('bundleCategory'$pluginConfig) && array_key_exists('bundelEnable'$pluginConfig) && $pluginConfig['bundelEnable']) {
  119.             $productDetailBundle = [
  120.                 'enable' => $pluginConfig['bundelEnable'],
  121.                 'groups' => [],
  122.                 'promotion' => ''
  123.             ];
  124.             $productGroupKeys = [
  125.                 'productGroupFirst',
  126.                 'productGroupSecond',
  127.                 'productGroupThird'
  128.             ];
  129.             foreach ($productGroupKeys as $productGroupKey) {
  130.                 $category null;
  131.                 if (!empty($pluginConfig[$productGroupKey])) {
  132.                     $filters $this->productStreamBuilder->buildFilters(
  133.                         $pluginConfig[$productGroupKey],
  134.                         $event->getContext()
  135.                     );
  136.                     $criteria = (new Criteria())
  137.                         ->addFilter(...$filters);
  138.                     $criteria->addFilter(new EqualsFilter('parentId'null));
  139.                     $criteria->addAssociations(['children''children.options''categories']);
  140.                     $products $this->productRepository->search($criteria$event->getSalesChannelContext())->getEntities();
  141.                     $criteria = new Criteria([$pluginConfig[$productGroupKey]]);
  142.                     /** @var ProductStreamEntity $productStream */
  143.                     $productStream $this->productStreamRepository->search($criteria$event->getContext())->first();
  144.                     foreach ($products as $newProduct) {
  145.                         foreach ($newProduct->getCategoryIds() as $value) {
  146.                             $category[] = $value;
  147.                         }
  148.                     }
  149.                     if ($category) {
  150.                         $category array_unique($category);
  151.                         if (!empty($category)) {
  152.                             $salesChannellCategory $this->categoryRepository->search(new Criteria($category), $event->getSalesChannelContext())->getEntities();
  153.                             $productStream->addExtension('category'$salesChannellCategory);
  154.                         }
  155.                     }
  156.                     $productStream->addExtension('products'$products);
  157.                     $productDetailBundle['groups'][] = $productStream;
  158.                 }
  159.                 if (!empty($pluginConfig['bundlePromotion'])) {
  160.                     $criteria = new Criteria([$pluginConfig['bundlePromotion']]);
  161.                     $criteria->addAssociation('discounts');
  162.                     /** @var PromotionEntity $promotion */
  163.                     $promotion $this->promotionRepository->search($criteria$event->getContext())->first();
  164.                     $promotion->addExtension('discount'$promotion->getDiscounts()->first());
  165.                     $productDetailBundle['promotion'] = $promotion;
  166.                 }
  167.             }
  168.             $event->getPage()->assign(['productDetailBundle' => $productDetailBundle]);
  169.         }
  170.     }
  171. }