vendor/store.shopware.com/ennodigitalproducts/Subscriber/ProductLoaded.php line 72

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace EnnoDigitalProducts\Subscriber;
  3. use EnnoDigitalProducts\Service\DigitalProductDataService;
  4. use Shopware\Core\Content\Product\ProductEntity;
  5. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Shopware\Core\Content\Product\ProductEvents;
  8. use Shopware\Core\System\SystemConfig\SystemConfigService;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\Context;
  12. class ProductLoaded implements EventSubscriberInterface
  13. {
  14.     /**
  15.      * @var SystemConfigService
  16.      */
  17.     private $systemConfigService;
  18.     private $digitalProductDataservice;
  19.     private $salesChannelRepository;
  20.     private $container;
  21.     private $salesChannelId;
  22.     private $salesChannel;
  23.     private $pluginConfig;
  24.     public function __construct(SystemConfigService $systemConfigServiceDigitalProductDataService $digitalProductDataserviceEntityRepositoryInterface $salesChannelRepository$container)
  25.     {
  26.         $this->systemConfigService $systemConfigService;
  27.         $this->digitalProductDataservice $digitalProductDataservice;
  28.         $this->salesChannelRepository $salesChannelRepository;
  29.         $this->container $container;
  30.     }
  31.     public static function getSubscribedEvents(): array
  32.     {
  33.         return [
  34.             ProductEvents::PRODUCT_LOADED_EVENT => 'onProductsLoaded'
  35.         ];
  36.     }
  37.     private function ennoInit()
  38.     {
  39.         $salesChannelId null;
  40.         try
  41.         {
  42.             $requestStack $this->container->get('request_stack');
  43.             if ( $requestStack && method_exists($requestStack'getCurrentRequest') )
  44.             {
  45.                 $currentRequest $requestStack->getCurrentRequest();
  46.                 if ($currentRequest && method_exists($currentRequest'get') )
  47.                 {
  48.                     $salesChannelId $currentRequest->get("sw-sales-channel-id");
  49.                 }
  50.             }
  51.         }
  52.         catch (\Exception $e) { }
  53.         if( !$salesChannelId ) return;
  54.         if( $this->salesChannelId === $salesChannelId ) return;
  55.         $this->salesChannelId $salesChannelId;
  56.         $this->salesChannel   $this->salesChannelRepository->search(new Criteria([$salesChannelId]), Context::createDefaultContext())->get($salesChannelId);
  57.         $this->pluginConfig   $this->systemConfigService->get('EnnoDigitalProducts.config'$salesChannelId);
  58.     }
  59.     public function onProductsLoaded(EntityLoadedEvent $event): void
  60.     {
  61.         $this->ennoInit();
  62.         if( !is_array($this->pluginConfig) ) return;
  63.         if( empty($this->pluginConfig) ) return;
  64.         if( !array_key_exists('active'$this->pluginConfig)) return;
  65.         if( !$this->pluginConfig["active"] ) return;
  66.         /** @var ProductEntity $productEntity */
  67.         foreach ($event->getEntities() as $entity)
  68.         {
  69.             $digitalData $this->digitalProductDataservice->getProductData($event->getContext(), $entity->getId(), $entity->getParentId());
  70.             if($digitalData$entity->addExtension('ennoDigitalProducts'$digitalData);
  71.             // die();
  72.         }
  73.     }
  74. }