custom/static-plugins/NdSRS/src/Subscriber/AddProductGroupData.php line 45

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nd\Srs\Subscriber;
  3. use DateTime;
  4. use Nd\Coef\Service\ProductManufacturerUrlService;
  5. use Shopware\Core\Content\Product\Aggregate\ProductMedia\ProductMediaEntity;
  6. use Shopware\Core\Content\Product\ProductEntity;
  7. use Shopware\Core\Content\Product\SalesChannel\Detail\ProductConfiguratorLoader;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Entity;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\Struct\ArrayStruct;
  15. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  16. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  17. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  18. use Shopware\Core\Content\Product\ProductEvents;
  19. class AddProductGroupData implements EventSubscriberInterface
  20. {
  21.     private EventDispatcherInterface $dispatcher;
  22.     private EntityRepository $productRepository;
  23.     private array $results;
  24.     private bool $isLoading;
  25.     public function __constructEventDispatcherInterface $dispatcherEntityRepository $productRepository)
  26.     {
  27.         $this->dispatcher $dispatcher;
  28.         $this->productRepository $productRepository;
  29.         $this->results = [];
  30.         $this->isLoading false;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34. //        return [];
  35.                 return [
  36.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded'
  37.         ];
  38.     }
  39.     public function onProductsLoaded(EntityLoadedEvent $event): void
  40.     {
  41.         if($this->isLoading){
  42.             return;
  43.         }
  44.         /** @var ProductEntity $productEntity */
  45.         foreach ($event->getEntities() as $productEntity) {
  46.             if(in_array($productEntity->getId(), array_keys($this->results))) {
  47.                 $group $this->results[$productEntity->getId()];
  48.             }else{
  49.                 $group $this->getGroup($productEntity$event->getContext());
  50.                 $this->results[$productEntity->getId()] = $group;
  51.             }
  52.             if ($group) {
  53.                 $productEntity->addExtension('productGroup', new ArrayStruct($group));
  54.             }
  55.         }
  56.     }
  57.     public function getGroup(ProductEntity $productEntityContext $context){
  58.         if(!$productEntity->getExtension('srsExtension')){
  59.             return false;
  60.         }
  61.         $srsData $productEntity->getExtension('srsExtension');
  62.         if(!$srsData->getBaseId()){
  63.             return false;
  64.         }
  65.         $baseId $srsData->getBaseId();
  66.         $criteria = new Criteria();
  67.         $criteria->addFilter(new EqualsFilter('product.srsExtension.baseId'$baseId));
  68.         $criteria->addFilter(new EqualsFilter('product.parentId'null));
  69.         $criteria->addAssociation('srsExtension');
  70.         $criteria->addAssociation('manufacturer');
  71.         $criteria->addAssociation('media');
  72.         $criteria->addAssociation('options.group');
  73.         $criteria->addAssociation('cover');
  74.         $criteria->addAssociation('properties.group');
  75.         $this->isLoading true;
  76.         $products $this->productRepository->search($criteria$context)->getEntities();
  77.         $this->isLoading false;
  78.         if($products->count() < 2){
  79.             return false;
  80.         }
  81.         return [$products];
  82.     }
  83. }