custom/plugins/KilbShopwareProductDesigner/src/Subscriber/ProductSubscriber.php line 46

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Kilb\KilbShopwareProductDesigner\Subscriber;
  3. use Kilb\KilbShopwareProductDesigner\ArrayCache\ArrayCacheTrait;
  4. use Kilb\KilbShopwareProductDesigner\Services\ConfigurationServiceInterface;
  5. use Shopware\Core\Content\Product\Events\ProductListingResultEvent;
  6. use Shopware\Core\Content\Product\ProductEntity;
  7. use Shopware\Core\Framework\Context;
  8. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntitySearchResultLoadedEvent;
  9. use Shopware\Storefront\Page\Product\ProductPageLoadedEvent;
  10. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  11. class ProductSubscriber implements EventSubscriberInterface
  12. {
  13.     use ArrayCacheTrait;
  14.     /**
  15.      * @var ConfigurationServiceInterface
  16.      */
  17.     private $configurationService;
  18.     /**
  19.      * @param ConfigurationServiceInterface $configurationService
  20.      */
  21.     public function __construct(ConfigurationServiceInterface $configurationService)
  22.     {
  23.         $this->configurationService $configurationService;
  24.     }
  25.     /**
  26.      * @return string[]
  27.      */
  28.     public static function getSubscribedEvents(): array
  29.     {
  30.         return[
  31.             ProductPageLoadedEvent::class => 'onProductPageLoaded',
  32.             ProductListingResultEvent::class => 'onProductListingResult',
  33.             'product.search.result.loaded' => 'onEntitySearchResultLoaded',
  34.         ];
  35.     }
  36.     /**
  37.      * @param EntitySearchResultLoadedEvent $event
  38.      */
  39.     public function onEntitySearchResultLoaded(EntitySearchResultLoadedEvent $event): void
  40.     {
  41.         foreach ($event->getResult()->getEntities()->getElements() as $product) {
  42.             $this->addCustomFields($product$event->getContext());
  43.         }
  44.     }
  45.     /**
  46.      * @param ProductPageLoadedEvent $event
  47.      */
  48.     public function onProductPageLoaded(ProductPageLoadedEvent $event): void
  49.     {
  50.        $product $event->getPage()->getProduct();
  51.         $this->addCustomFields($product$event->getContext());
  52.     }
  53.     /**
  54.      * @param ProductListingResultEvent $event
  55.      */
  56.     public function onProductListingResult(ProductListingResultEvent $event): void
  57.     {
  58.         foreach ($event->getResult()->getElements() as $product) {
  59.             $this->addCustomFields($product$event->getContext());
  60.         }
  61.     }
  62.     /**
  63.      * @param ProductEntity $product
  64.      * @param Context $context
  65.      */
  66.     private function addCustomFields(
  67.         ProductEntity $product,
  68.         Context $context
  69.     ): void {
  70.         $hasProductDesign false;
  71.         $isBasketButtonDisabled false;
  72.         $configurations $this->configurationService->getConfigurationsByProductNumber(
  73.             $product->getProductNumber(),
  74.             $context
  75.         );
  76.         if (!empty($configurations)) {
  77.             $configuration array_shift($configurations);
  78.             $hasProductDesign true;
  79.             $isBasketButtonDisabled $configuration['isBasketButtonDisabled'];
  80.         }
  81.         $customFields $product->getCustomFields();
  82.         $customFields['hasProductDesign'] = $hasProductDesign;
  83.         $customFields['isBasketButtonDisabled'] = $isBasketButtonDisabled;
  84.         $product->setCustomFields($customFields);
  85.     }
  86. }