custom/plugins/LogBchange/src/Subscriber/ProductPageSubscriber.php line 51

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace LogBchange\Subscriber;
  3. use LogBchange\Service\HighlightPropertyService;
  4. use Shopware\Core\Content\Cms\CmsPageCollection;
  5. use Shopware\Core\Content\Cms\SalesChannel\SalesChannelCmsPageLoaderInterface;
  6. use Shopware\Core\Content\Media\MediaCollection;
  7. use Shopware\Core\Content\Product\Events\ProductCrossSellingIdsCriteriaEvent;
  8. use Shopware\Core\Content\Product\ProductEntity;
  9. use Shopware\Core\Content\Product\SalesChannel\CrossSelling\CrossSellingElement;
  10. use Shopware\Core\Framework\Context;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Shopware\Storefront\Page\Product\ProductPage;
  16. use Shopware\Storefront\Page\Product\ProductPageCriteriaEvent;
  17. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  18. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  19. use Symfony\Component\HttpFoundation\Request;
  20. class ProductPageSubscriber implements EventSubscriberInterface
  21. {
  22.     private EntityRepositoryInterface $mediaRepository;
  23.     private SalesChannelCmsPageLoaderInterface $cmsPageLoader;
  24.     private HighlightPropertyService $highlightPropertyService;
  25.     public function __construct(EntityRepositoryInterface $mediaRepository, SalesChannelCmsPageLoaderInterface $cmsPageLoader, HighlightPropertyService $highlightPropertyService)
  26.     {
  27.         $this->mediaRepository = $mediaRepository;
  28.         $this->cmsPageLoader = $cmsPageLoader;
  29.         $this->highlightPropertyService = $highlightPropertyService;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             ProductPageCriteriaEvent::class => 'onProductPageCriteria',
  35.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  36.             ProductCrossSellingIdsCriteriaEvent::class => 'onProductCrossSellingIdsCriteria',
  37.         ];
  38.     }
  39.     public function onProductPageCriteria(ProductPageCriteriaEvent $event): void
  40.     {
  41.         $event->getCriteria()->addAssociation('properties.media');
  42.     }
  43.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  44.     {
  45.         $page = $event->getPage();
  46.         $context = $event->getContext();
  47.         $request = $event->getRequest();
  48.         $saleChannelContext = $event->getSalesChannelContext();
  49.         $this->enrichHighlightProperties($page);
  50.         $this->enrichSidebarMedia($page, $context);
  51.         $this->enrichSocialMediaImages($page, $context);
  52.         $this->enrichCmsPageLayout($page, $saleChannelContext, $request);
  53.     }
  54.     public function onProductCrossSellingIdsCriteria(ProductCrossSellingIdsCriteriaEvent $event): void
  55.     {
  56.         $criteria = $event->getCriteria();
  57.         $criteria->addAssociation('properties.group');
  58.     }
  59.     private function enrichHighlightProperties(ProductPage $page): void
  60.     {
  61.         $product = $page->getProduct();
  62.         $crossSellings = $page->getCrossSellings();
  63.         $this->highlightPropertyService->enrichHighlightProperties($product);
  64.         /** @var CrossSellingElement $crossSelling */
  65.         foreach ($crossSellings as $crossSelling) {
  66.             /** @var ProductEntity $product */
  67.             foreach ($crossSelling->getProducts() as $product) {
  68.                 $this->highlightPropertyService->enrichHighlightProperties($product);
  69.             }
  70.         }
  71.     }
  72.     private function enrichSidebarMedia(ProductPage $page, Context $context): void
  73.     {
  74.         $product = $page->getProduct();
  75.         $sidebarMediaIds = array_filter([
  76.             $product->getCustomFields()['custom_product_fields_extra_image1'] ?? null,
  77.             $product->getCustomFields()['custom_product_fields_extra_image2'] ?? null,
  78.             $product->getCustomFields()['custom_product_fields_extra_image3'] ?? null,
  79.         ]);
  80.         $sidebarMedia = $sidebarMediaIds ? $this->mediaRepository->search(new Criteria($sidebarMediaIds), $context)->getEntities() : new MediaCollection();
  81.         $product->addExtension('sidebarMedia', $sidebarMedia);
  82.     }
  83.     private function enrichSocialMediaImages(ProductPage $page, Context $context): void
  84.     {
  85.         $product = $page->getProduct();
  86.         $socialMediaImageFacebookId = $product->getCustomFields()['custom_social_media_images_facebook_de'] ?? null;
  87.         $socialMediaImageTwitterId = $product->getCustomFields()['custom_social_media_images_twitter_de'] ?? null;
  88.         $socialMediaImageIds = array_filter([
  89.             $socialMediaImageFacebookId,
  90.             $socialMediaImageTwitterId,
  91.         ]);
  92.         
  93.         $socialMediaImages = $socialMediaImageIds ? $this->mediaRepository->search(new Criteria($socialMediaImageIds), $context)->getEntities() : new MediaCollection();
  94.         $product->addArrayExtension('socialMediaImages', [
  95.             'facebook' => $socialMediaImages->get($socialMediaImageFacebookId),
  96.             'twitter' => $socialMediaImages->get($socialMediaImageTwitterId),
  97.         ]);
  98.     }
  99.     private function enrichCmsPageLayout(ProductPage $page, SalesChannelContext $context, Request $request): void
  100.     {
  101.         $product = $page->getProduct();
  102.         $cmsPageID = $product->getCustomFields()['custom_product_fields_landing_page'] ?? null;
  103.         if($cmsPageID){
  104.             $pages = $this->cmsPageLoader->load(
  105.                 $request,
  106.                 $this->createCriteria($cmsPageID, $request),
  107.                 $context,
  108.                 null,
  109.                 null
  110.             );
  111.             if($pages->has($cmsPageID)){
  112.                 $product->addArrayExtension('landingPageLayout', [
  113.                     'landingPage' => $pages->get($cmsPageID)
  114.                 ]);
  115.             }
  116.         }
  117.         else {
  118.             $product->addArrayExtension('landingPageLayout', [
  119.                 'landingPage' => null
  120.             ]);
  121.         }
  122.        /* $layoutIDs = array_filter([$layoutID]);
  123.         $cmsPageLayout = $layoutIDs ? $this->cmsPageLoader->search(new Criteria($layoutIDs), $context)->get($layoutID) : new CmsPageCollection();
  124.         echo print_r("landing page => " . $cmsPageLayout->getSections(), true);
  125.         $product->addArrayExtension('landingPageLayout', [
  126.             'landingPage' => $cmsPageLayout
  127.         ]);*/
  128.     }
  129.     private function createCriteria(string $pageId, Request $request): Criteria
  130.     {
  131.         $criteria = new Criteria([$pageId]);
  132.         $criteria->setTitle('landing-page::cms-page');
  133.         $slots = $request->get('slots');
  134.         if (\is_string($slots)) {
  135.             $slots = explode('|', $slots);
  136.         }
  137.         if (!empty($slots) && \is_array($slots)) {
  138.             $criteria
  139.                 ->getAssociation('sections.blocks')
  140.                 ->addFilter(new EqualsAnyFilter('slots.id', $slots));
  141.         }
  142.         return $criteria;
  143.     }
  144. }