custom/plugins/TwigelStrengthPlugin/src/Subscriber/ProductDetailSubscriber.php line 90

Open in your IDE?
  1. <?php
  2. namespace Twigel\StrengthPlugin\Subscriber;
  3. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  4. use Shopware\Core\Content\Property\Aggregate\PropertyGroupOption\PropertyGroupOptionEntity;
  5. use Shopware\Core\Content\Property\PropertyGroupDefinition;
  6. use Shopware\Core\Content\Property\PropertyGroupEntity;
  7. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Twigel\StrengthPlugin\Service\TwigelStrengthGroupOptions;
  14. class ProductDetailSubscriber implements EventSubscriberInterface
  15. {
  16.     /** @var SystemConfigService */
  17.     protected $config;
  18.     /** @var EntityRepository */
  19.     protected $groupRepository;
  20.     public function __construct(SystemConfigService $configEntityRepository $groupRepository)
  21.     {
  22.         $this->config $config;
  23.         $this->groupRepository $groupRepository;
  24.     }
  25.     /**
  26.      * @inheritDoc
  27.      */
  28.     public static function getSubscribedEvents()
  29.     {
  30.         return [
  31.             ProductPageLoadedEvent::class => 'productLoaded',
  32.             ProductListingResultEvent::class => 'listing'
  33.         ];
  34.     }
  35.     public function listing(ProductListingResultEvent $event){
  36.         $products $event->getResult()->getElements();
  37.         $pluginConfig $this->config->get("TwigelStrengthPlugin");
  38.         if (!is_null($pluginConfig)){
  39.             $pluginConfig $pluginConfig['config'];
  40.         }
  41.         foreach ($products as $product){
  42.             $productPropertyIds $product->getPropertyIds();
  43.             $strength = [];
  44.             if (!is_null($productPropertyIds) && !is_null($pluginConfig)){
  45.                 $strength array_intersect($productPropertyIds$pluginConfig);
  46.                 if ($strength){
  47.                     $criteria = new Criteria();
  48.                     $criteria->addFilter(new EqualsFilter('name'TwigelStrengthGroupOptions::POPERS_STRENGTHS));
  49.                     $criteria->addAssociations(['options']);
  50.                     /** @var PropertyGroupEntity $group */
  51.                     $group $this->groupRepository->search($criteria$event->getContext())->first();
  52.                     if (!$group){
  53.                         return null;
  54.                     }
  55.                     $options = [];
  56.                     /** @var PropertyGroupOptionEntity $option */
  57.                     foreach ($group->getOptions() as $option){
  58.                         $options[$option->getTranslation('name')] = [
  59.                             'name' => $option->getTranslation('name'),
  60.                             'id' => $option->getId(),
  61.                             'groupId' => $option->getGroupId(),
  62.                             'active' => false
  63.                         ];
  64.                         if ($strength[0] === $option->getId()){
  65.                             $options[$option->getTranslation('name')]['active'] = true;
  66.                         }
  67.                     }
  68.                     krsort($options);
  69.                     $product->setExtensions(['options' => $options]);
  70.                 }
  71.             }
  72.         }
  73.     }
  74.     public function productLoaded(ProductPageLoadedEvent $event)
  75.     {
  76.         $productPropertyIds $event->getPage()->getProduct()->getPropertyIds();
  77.         $pluginConfig $this->config->get("TwigelStrengthPlugin");
  78.         $strength = [];
  79.         if (!is_null($pluginConfig)){
  80.             $pluginConfig $pluginConfig['config'];
  81.         }
  82.         if (!is_null($productPropertyIds) && !is_null($pluginConfig)){
  83.             $strength array_intersect($productPropertyIds$pluginConfig);
  84.             if ($strength){
  85.                 $criteria = new Criteria();
  86.                 $criteria->addFilter(new EqualsFilter('name'TwigelStrengthGroupOptions::POPERS_STRENGTHS));
  87.                 $criteria->addAssociations(['options']);
  88.                 /** @var PropertyGroupEntity $group */
  89.                 $group $this->groupRepository->search($criteria$event->getContext())->first();
  90.                 if (!$group){
  91.                     return null;
  92.                 }
  93.                 $options = [];
  94.                 /** @var PropertyGroupOptionEntity $option */
  95.                 foreach ($group->getOptions() as $option){
  96.                     $options[$option->getTranslation('name')] = [
  97.                         'name' => $option->getTranslation('name'),
  98.                         'id' => $option->getId(),
  99.                         'groupId' => $option->getGroupId(),
  100.                         'active' => false
  101.                     ];
  102.                     if ($strength[0] === $option->getId()){
  103.                         $options[$option->getTranslation('name')]['active'] = true;
  104.                     }
  105.                 }
  106.                 krsort($options);
  107.                 $event->getPage()->assign(['strength' => true'strengthOptions' => $options ]);
  108.             }
  109.         }
  110.     }
  111. }