custom/plugins/TTPropertyCategory/src/Subscriber/FrontendListingSubscriber.php line 110

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace TT\PropertyCategory\Subscriber;
  4. use Shopware\Core\Content\Product\Events\ProductListingCriteriaEvent;
  5. use Shopware\Core\Content\Product\Events\ProductSearchCriteriaEvent;
  6. use Shopware\Core\Content\Product\Events\ProductListingCollectFilterEvent;
  7. use Shopware\Core\Content\Product\SalesChannel\Listing\Filter;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\MultiFilter;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\RangeFilter;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Bucket\FilterAggregation;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\MaxAggregation;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Aggregation\Metric\StatsAggregation;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Shopware\Core\Content\Category\CategoryEvents;
  16. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityWrittenEvent;
  17. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  18. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  19. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsAnyFilter;
  20. use Symfony\Component\HttpFoundation\Request;
  21. class FrontendListingSubscriber implements EventSubscriberInterface
  22. {
  23.     protected EntityRepositoryInterface $categoryRepository;
  24.     protected EntityRepositoryInterface $propertyGroupRepository;
  25.     protected EntityRepositoryInterface $customFieldRepository;
  26.     public function __construct(
  27.         EntityRepositoryInterface $categoryRepository,
  28.         EntityRepositoryInterface $propertyGroupRepository,
  29.         EntityRepositoryInterface $customFieldRepository
  30.     ) {
  31.         $this->categoryRepository $categoryRepository;
  32.         $this->propertyGroupRepository $propertyGroupRepository;
  33.         $this->customFieldRepository $customFieldRepository;
  34.     }
  35.     public static function getSubscribedEvents()
  36.     {
  37.         return [
  38.             ProductListingCriteriaEvent::class => 'handleListingRequest',
  39.             ProductSearchCriteriaEvent::class => 'handleListingRequest',
  40.             CategoryEvents::CATEGORY_WRITTEN_EVENT => 'checkPropertiesLimitValid',
  41.             ProductListingCollectFilterEvent::class => 'handleAddFilter'
  42.         ];
  43.     }
  44.     public function handleListingRequest(ProductListingCriteriaEvent $event): void
  45.     {
  46.         $event->getCriteria()->addAssociation('properties.group');
  47.     }
  48.     public function checkPropertiesLimitValid(EntityWrittenEvent $event): void
  49.     {
  50.         $catId null;
  51.         foreach ($event->getWriteResults() as $result) {
  52.             $catId $result->getPrimaryKey();
  53.         }
  54.         
  55.         if (!empty($catId)) {
  56.             $categoryCriteria = new Criteria([$catId]);
  57.             $category $this->categoryRepository->search($categoryCriteria$event->getContext())->first();
  58.             if ($category) {
  59.                 $customFields $category->getCustomFields();
  60.                 if (isset($customFields['tt_category_property_limitation'])) {
  61.                     $propertiesIds explode('|'$customFields['tt_category_property_limitation']);
  62.                     $propertiesIds array_filter($propertiesIds);
  63.                     if (!empty($propertiesIds)) {
  64.                         $propertiesFilterIds $this->propertyGroupRepository->searchIds(new Criteria($propertiesIds), $event->getContext())->getIds();
  65.                         //Update list property limit when list id property difference list id in custom_field
  66.                         if ($propertiesIds != $propertiesFilterIds) {
  67.                             $saveData = [
  68.                                 'tt_category_property_limitation' => implode('|'$propertiesFilterIds)
  69.                             ];
  70.                             $this->categoryRepository->update([
  71.                                 [
  72.                                  'id' => $catId,
  73.                                  'customFields' => $saveData
  74.                                 ]
  75.                             ], $event->getContext());
  76.                         }
  77.                     }
  78.                 }
  79.                 if (isset($customFields['tt_category_custom_field_limitation'])) {
  80.                     $customFieldIds explode('|'$customFields['tt_category_custom_field_limitation']);
  81.                     $customFieldIds array_filter($customFieldIds);
  82.                     if (!empty($customFieldIds)) {
  83.                         $customFieldFilterIds $this->customFieldRepository->searchIds(new Criteria($customFieldIds), $event->getContext())->getIds();
  84.                         if ($customFieldFilterIds != $customFieldIds) {
  85.                             $saveData = [
  86.                                 'tt_category_property_limitation' => implode('|'$customFieldFilterIds)
  87.                             ];
  88.                             $this->categoryRepository->update([
  89.                                 [
  90.                                  'id' => $catId,
  91.                                  'customFields' => $saveData
  92.                                 ]
  93.                             ], $event->getContext());
  94.                         }
  95.                     }
  96.                 }
  97.             }
  98.         }
  99.     }
  100.     public function handleAddFilter(ProductListingCollectFilterEvent $event): void
  101.     {
  102.         // fetch existing filters
  103.         $filters $event->getFilters();
  104.         $request $event->getRequest();
  105.         $filterName 'masse-lxbxh';
  106.         $filters->add($this->getFilter($filterName$request));
  107.         if (!$request->request->get($filterNametrue)) {
  108.             $filters->remove($filterName);
  109.         }
  110.     }
  111.     private function getFilter(string $nameRequest $request): Filter
  112.     {
  113.         $queryString $request->get($name);
  114.         $length = [];
  115.         $width  = [];
  116.         $height = [];
  117.         if (!empty($queryString)) {
  118.             if (strpos($queryString"|") !== false) {
  119.                 $listMasse explode('|'$queryString);
  120.             } else {
  121.                 $listMasse explode(','$queryString);
  122.             }
  123.             foreach ($listMasse as $masse) {
  124.                 $masse str_replace(['-''mm'' '], ''$masse);
  125.                 $arr explode('x'$masse);
  126.                 $length[] = $arr[0];
  127.                 $width[]  = $arr[1];
  128.                 $height[] = $arr[2];
  129.             }
  130.         }
  131.         $filter = [];
  132.         if(!empty($length)) {
  133.             $filter[] = new EqualsAnyFilter('product.length'$length);
  134.         }
  135.         if(!empty($width)) {
  136.             $filter[] = new EqualsAnyFilter('product.width'$width);
  137.         }
  138.         if(!empty($height)) {
  139.             $filter[] = new EqualsAnyFilter('product.height'$height);
  140.         }
  141.         return new Filter(
  142.             $name,
  143.             !empty($queryString),
  144.             [],
  145.             new MultiFilter(
  146.                 MultiFilter::CONNECTION_AND,
  147.                 $filter
  148.             ),
  149.             $queryString
  150.         );
  151.     }
  152. }