custom/plugins/FourtwosixCategoryDescriptionOnPDP/src/Subscriber/AddCategoryDescription2PDPSubscriber.php line 41

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace FourtwosixCategoryDescriptionOnPDP\Subscriber;
  3. use Shopware\Core\Content\Category\CategoryEntity;
  4. use Shopware\Core\Framework\Context;
  5. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  6. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  7. use Shopware\Core\Framework\DataAbstractionLayer\Search\EntitySearchResult;
  8. use Shopware\Core\Framework\Struct\ArrayStruct;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class AddCategoryDescription2PDPSubscriber implements EventSubscriberInterface
  12. {
  13.     protected EntityRepositoryInterface $categoryRepository;
  14.     /**
  15.      * @param EntityRepositoryInterface $categoryRepository
  16.      */
  17.     public function __construct(
  18.         EntityRepositoryInterface $categoryRepository
  19.     ) {
  20.         $this->categoryRepository $categoryRepository;
  21.     }
  22.     /**
  23.      * @return string[]
  24.      */
  25.     public static function getSubscribedEvents(): array
  26.     {
  27.         return [
  28.             ProductPageLoadedEvent::class => 'onProductsLoaded',
  29.         ];
  30.     }
  31.     /**
  32.      * @param ProductPageLoadedEvent $event
  33.      * @return void
  34.      */
  35.     public function onProductsLoaded(ProductPageLoadedEvent $event): void
  36.     {
  37.         $context $event->getContext();
  38.         $nonSeoCategoryIds $event->getPage()->getProduct()->getCategoryIds();
  39.         $seoCategory $event->getPage()->getProduct()->getSeoCategory();
  40.         // If not found there is something wrong skip to avoid errors, no extension will be set
  41.         if (is_null($seoCategory) || is_null($nonSeoCategoryIds)) {
  42.             return;
  43.         }
  44.         $seoCategoryPath $seoCategory->getPath()."|".$seoCategory->getId();
  45.         $seoCategoryIds array_reverse(array_filter(explode("|"$seoCategoryPath)));
  46.         // Remove seo category from nonSeoCategory to avoid duplicate values
  47.         $seoCategoryIndex array_search($seoCategory->getId(), $nonSeoCategoryIds);
  48.         unset($nonSeoCategoryIds[$seoCategoryIndex]);
  49.         // Get sorted categories id from non seo categories using path if more than one category assigned to product
  50.         if ($nonSeoCategoryIds) {
  51.             $nonSeoCategoryEntities $this->getEntities($nonSeoCategoryIds$context);
  52.             $sortedNonSeoCategoriesId $this->sortCategoriesWithPath($nonSeoCategoryEntities);
  53.             $sortedCategories array_merge($seoCategoryIds$sortedNonSeoCategoriesId);
  54.         } else {
  55.             // Avoid loading all categories if nonSeoCategoryIds is empty
  56.             $sortedCategories $seoCategoryIds;
  57.         }
  58.         // We want seoCategoryIds to be processed first
  59.         $allCategoriesFromPath $this->getEntities($sortedCategories$context);
  60.         /** @var CategoryEntity $categoryFromPath */
  61.         foreach ($allCategoriesFromPath->getEntities() as $categoryFromPath) {
  62.             $description $categoryFromPath->getTranslated()["description"] ?? null;
  63.             if (!is_null($description) && $description !== '') {
  64.                 $event->getPage()->addExtension(
  65.                     "FourtwosixCategoryDescriptionPLP",
  66.                     new ArrayStruct([$description])
  67.                 );
  68.                 return;
  69.             }
  70.         }
  71.     }
  72.     /**
  73.      * @param array   $categoryIds
  74.      * @param Context $context
  75.      * @return EntitySearchResult
  76.      */
  77.     protected function getEntities(array $categoryIdsContext $context): EntitySearchResult
  78.     {
  79.         $criteria = new Criteria($categoryIds);
  80.         return $this->categoryRepository->search($criteria$context);
  81.     }
  82.     /**
  83.      * @param EntitySearchResult $nonSeoCategoryEntities
  84.      * @return array
  85.      */
  86.     protected function sortCategoriesWithPath(EntitySearchResult $nonSeoCategoryEntities): array
  87.     {
  88.         $orderedCategories = [];
  89.         /** @var CategoryEntity $entity */
  90.         foreach ($nonSeoCategoryEntities->getEntities() as $entity) {
  91.             // Current Category is not present in path
  92.             $entityPath $entity->getPath()."|{$entity->id}";
  93.             $path array_filter(explode("|"$entityPath));
  94.             $orderedCategories = [...$orderedCategories, ...$path];
  95.         }
  96.         return $orderedCategories;
  97.     }
  98. }