custom/plugins/SchilderSysteme/src/Subscriber/ListingSubscriber.php line 142

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace SchilderSysteme\Subscriber;
  4. use SchilderSysteme\Traits\ThemeTrait;
  5. use Shopware\Core\Content\Category\CategoryCollection;
  6. use Shopware\Core\Content\Category\CategoryEntity;
  7. use Shopware\Core\Content\Cms\Aggregate\CmsSlot\CmsSlotEntity;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Storefront\Page\Navigation\NavigationPageLoadedEvent;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. class ListingSubscriber implements EventSubscriberInterface
  15. {
  16.     use ThemeTrait;
  17.     private $productRepository;
  18.     private $categoryRepository;
  19.     private $themeRepository;
  20.     public function __construct(
  21.         EntityRepositoryInterface $productRepository,
  22.         EntityRepositoryInterface $categoryRepository,
  23.         EntityRepositoryInterface $themeRepository
  24.     )
  25.     {
  26.         $this->productRepository $productRepository;
  27.         $this->categoryRepository $categoryRepository;
  28.         $this->themeRepository $themeRepository;
  29.     }
  30.     public static function getSubscribedEvents(): array
  31.     {
  32.         return [
  33.             NavigationPageLoadedEvent::class => [
  34.                 ['updateProductVariants'],
  35.                 ['addSubcategories'],
  36.                 ['updateActiveCategoryAssociations']
  37.             ]
  38.         ];
  39.     }
  40.     /**
  41.      * Add all variants info for displayed products
  42.      *
  43.      * @param NavigationPageLoadedEvent $event
  44.      */
  45.     public function updateProductVariants(NavigationPageLoadedEvent $event)
  46.     {
  47.         if(!$this->isCorrectTheme($event->getContext(), $event->getSalesChannelContext(), $this->themeRepository)){
  48.             return;
  49.         }
  50.         $slots $event->getPage()->getCmsPage()->getSections()->getBlocks()->getSlots();
  51.         $context $event->getContext();
  52.         foreach($slots as $slot){
  53.             if($slot->getType() == 'product-listing'){
  54.                 $this->updateProducts($slot$context);
  55.             }
  56.         }
  57.     }
  58.     private function updateProducts(CmsSlotEntity $slotContext $context)
  59.     {
  60.         $products $slot->getData()->getListing()->getEntities()->getElements();
  61.         foreach($products as $product){
  62.             if($product->getParentId()){
  63.                 $this->addVariants($product$context);
  64.             }
  65.         }
  66.     }
  67.     private function addVariants($productContext $context)
  68.     {
  69.         $variants $this->getVariants($product->getParentId(), $context);
  70.         $customFields $product->getCustomFields();
  71.         $customFields['sz_variants'] = $variants;
  72.         $product->setCustomFields($customFields);
  73.     }
  74.     private function getVariants($parentIdContext $context)
  75.     {
  76.         $parentFilter = new EqualsFilter('parentId'$parentId);
  77.         $criteria = (new Criteria())
  78.             ->addFilter($parentFilter)
  79.             ->addAssociation('cover');
  80.         return $this->productRepository->search($criteria$context)->getElements();
  81.     }
  82.     /**
  83.      * Add list of subcategories and subcategorie's products for current category page
  84.      *
  85.      * @param NavigationPageLoadedEvent $event
  86.      */
  87.     public function addSubcategories(NavigationPageLoadedEvent $event)
  88.     {
  89.         if(!$this->isCorrectTheme($event->getContext(), $event->getSalesChannelContext(), $this->themeRepository)){
  90.             return;
  91.         }
  92.         $slots $event->getPage()->getCmsPage()->getSections()->getBlocks()->getSlots();
  93.         $context $event->getContext();
  94.         foreach($slots as $slot){
  95.             if($slot->getType() == 'product-listing'){
  96.                 $this->updateSubcategories($slot$context);
  97.             }
  98.         }
  99.     }
  100.     private function updateSubcategories(CmsSlotEntity $slotContext $context)
  101.     {
  102.         $parentNavigationId $slot->getData()->getListing()->getCurrentFilters()['navigationId'];
  103.         $parentCategory $slot->getFieldConfig()->get('category');
  104.         if (isset($parentCategory)) {
  105.             $parentNavigationId $parentCategory->getValue()[0];
  106.         }
  107.         $subcategories $this->getSubcategories($parentNavigationId$context);
  108.         $customFields $slot->getCustomFields();
  109.         $customFields['sz_subcategories'] = $subcategories;
  110.         $slot->setCustomFields($customFields);
  111.     }
  112.     private function getSubcategories($parentNavigationIdContext $context)
  113.     {
  114.         $parentFilter = new EqualsFilter('parentId'$parentNavigationId);
  115.         $criteria = (new Criteria())
  116.             ->addFilter($parentFilter)
  117.             ->addAssociations(['products''products.cover']);
  118.         return $this->categoryRepository->search($criteria$context)->getElements();
  119.     }
  120.     public function updateActiveCategoryAssociations(NavigationPageLoadedEvent $event)
  121.     {
  122.         $activeCategory $event->getPage()->getHeader()->getNavigation()->getActive();
  123.         $category $this->getUpdatedCategory($activeCategory->getId(), $event->getContext());
  124.         $event->getPage()->getHeader()->getNavigation()->setActive($category);
  125.     }
  126.     private function getUpdatedCategory($categoryId$context)
  127.     {
  128.         $criteria = (new Criteria([$categoryId]))->addAssociations(['children''parent']);
  129.         $category $this->categoryRepository->search($criteria$context)->first();
  130.         if ($category->getChildren()) {
  131.             $this->sortCategoryByAutoIncrement($category);
  132.         }
  133.         if ($category->getParentId()) {
  134.             $criteria = (new Criteria([$category->getParentId()]))->addAssociation('children');
  135.             $parentCategory $this->categoryRepository->search($criteria$context)->first();
  136.             if ($parentCategory->getChildren()) {
  137.                 $this->sortCategoryByAutoIncrement($parentCategory);
  138.             }
  139.             $category->setParent($parentCategory);
  140.         }
  141.         return $category;
  142.     }
  143.     private function sortCategoryByAutoIncrement(CategoryEntity $category)
  144.     {
  145.         $categoriesChildrensArray $category->getChildren()->getElements();
  146.         usort($categoriesChildrensArray, function($c1$c2) {
  147.             return $c1->getAutoIncrement() - $c2->getAutoIncrement();
  148.         });
  149.         $category->setChildren(new CategoryCollection($categoriesChildrensArray));
  150.     }
  151. }