custom/plugins/RecentlyViewedProduct-master/src/Subscriber/Storefront/CmsPageLoaderSubscriber.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace RecentlyViewedProduct\Subscriber\Storefront;
  3. use RecentlyViewedProduct\RecentlyViewedProduct;
  4. use RecentlyViewedProduct\Service\RecentlyViewedProductService;
  5. use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockCollection;
  6. use Shopware\Core\Content\Cms\Aggregate\CmsBlock\CmsBlockEntity;
  7. use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionCollection;
  8. use Shopware\Core\Content\Cms\Aggregate\CmsSection\CmsSectionEntity;
  9. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotCollection;
  10. use Shopware\Core\Content\Cms\CmsPageCollection;
  11. use Shopware\Core\Content\Cms\CmsPageEntity;
  12. use Shopware\Core\Content\Cms\Events\CmsPageLoadedEvent;
  13. use Shopware\Core\Framework\Uuid\Uuid;
  14. use Shopware\Core\System\SystemConfig\SystemConfigService;
  15. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  16. class CmsPageLoaderSubscriber implements EventSubscriberInterface
  17. {
  18.     /**
  19.      * @var RecentlyViewedProductService
  20.      */
  21.     private $recentlyViewedProductService;
  22.     /**
  23.      * @var SystemConfigService
  24.      */
  25.     private $systemConfigService;
  26.     public function __construct(
  27.         RecentlyViewedProductService $recentlyViewedProductService,
  28.         SystemConfigService $systemConfigService
  29.     ) {
  30.         $this->recentlyViewedProductService $recentlyViewedProductService;
  31.         $this->systemConfigService $systemConfigService;
  32.     }
  33.     public static function getSubscribedEvents(): array
  34.     {
  35.         return [
  36.             CmsPageLoadedEvent::class => 'resolveSalesChannelContext',
  37.         ];
  38.     }
  39.     public function resolveSalesChannelContext(CmsPageLoadedEvent $event): void
  40.     {
  41.         try {
  42.             $context $event->getSalesChannelContext();
  43.             /** @var CmsPageCollection $result */
  44.             $result $event->getResult();
  45.             /** @var array $pluginConfig */
  46.             $pluginConfig $this->systemConfigService->get(RecentlyViewedProduct::PLUGIN_NAME '.config'$context->getSalesChannel()->getId());
  47.             if (empty($pluginConfig) || $pluginConfig['autoShowOnCmsPage'] === false) {
  48.                 return;
  49.             }
  50.             if ($result->count() === || !$result->first() instanceof CmsPageEntity) {
  51.                 return;
  52.             }
  53.             /** @var CmsSectionCollection|null $cmsPageSections */
  54.             $cmsPageSections $result->first()->getSections();
  55.             if (!$cmsPageSections || $cmsPageSections->count() === 0) {
  56.                 return;
  57.             }
  58.             $products $this->recentlyViewedProductService->getRecentProductEntities($context);
  59.             if (!$products || $products->count() === 0) {
  60.                 return;
  61.             }
  62.             /** @var CmsSectionEntity $cmsPageSection */
  63.             $cmsPageSection $cmsPageSections->last();
  64.             $lastSectionBlocks $cmsPageSection->getBlocks();
  65.             if (!$lastSectionBlocks || $lastSectionBlocks->count() === 0) {
  66.                 return;
  67.             }
  68.             $pseudoSection CmsSectionEntity::createFrom($cmsPageSection);
  69.             $pseudoSection->setUniqueIdentifier(Uuid::randomHex());
  70.             $mainSectionBlocks $lastSectionBlocks->filter(function (CmsBlockEntity $cmsBlock) {
  71.                 return $cmsBlock->getSectionPosition() === 'main';
  72.             });
  73.             $referenceBlock $mainSectionBlocks->last();
  74.             if (!$referenceBlock) {
  75.                 return;
  76.             }
  77.             $pseudoSliderBlock CmsBlockEntity::createFrom($referenceBlock);
  78.             $pseudoSliderBlock->setType(RecentlyViewedProduct::RECENTLY_VIEWED_PRODUCT_TYPE);
  79.             $pseudoSliderBlock->setSectionId($cmsPageSection->getId());
  80.             $pseudoSliderBlock->setUniqueIdentifier(Uuid::randomHex());
  81.             $pseudoSliderBlock->setId(Uuid::randomHex());
  82.             $pseudoSlot $this->recentlyViewedProductService->buildPseudoElement($context);
  83.             $productSliderStruct $this->recentlyViewedProductService->buildRecentProductSliderStruct($context);
  84.             $pseudoSlot->setBlockId($pseudoSliderBlock->getId());
  85.             $pseudoSlot->setData($productSliderStruct);
  86.             $pseudoSliderBlock->setPosition(1);
  87.             $pseudoSliderBlock->setSlots(new CmsSlotCollection([$pseudoSlot]));
  88.             $pseudoSection->setBlocks(new CmsBlockCollection([$pseudoSliderBlock]));
  89.             $pseudoSection->setType('default');
  90.             $cmsPageSections->add($pseudoSection);
  91.         } catch (\Throwable $exception) {
  92.             // nth
  93.         }
  94.     }
  95. }