vendor/store.shopware.com/ennodigitalproducts/Subscriber/OrderLineItemLoaded.php line 66

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\Checkout\Cart\LineItem\LineItem;
  8. use Shopware\Core\Content\Product\ProductEvents;
  9. use Shopware\Core\Checkout\Order\OrderEvents;
  10. use Shopware\Core\System\SystemConfig\SystemConfigService;
  11. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  14. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  15. use Shopware\Core\Framework\Context;
  16. use Doctrine\DBAL\Connection;
  17. class OrderLineItemLoaded implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var SystemConfigService
  21.      */
  22.     private $systemConfigService;
  23.     private $digitalProductDataservice;
  24.     private $salesChannelRepository;
  25.     private $container;
  26.     private $salesChannelId;
  27.     private $salesChannel;
  28.     private $pluginConfig;
  29.     public function __construct(SystemConfigService $systemConfigServiceDigitalProductDataService $digitalProductDataserviceEntityRepositoryInterface $salesChannelRepository$container)
  30.     {
  31.         $this->systemConfigService $systemConfigService;
  32.         $this->digitalProductDataservice $digitalProductDataservice;
  33.         $this->salesChannelRepository $salesChannelRepository;
  34.         $this->container $container;
  35.     }
  36.     public static function getSubscribedEvents(): array
  37.     {
  38.         return [
  39.             OrderEvents::ORDER_LINE_ITEM_LOADED_EVENT => 'onOrderLineItemLoaded'
  40.         ];
  41.     }
  42.     private function ennoInit(EntityLoadedEvent $event): void
  43.     {
  44.         $lineItemIds $event->getIds();
  45.         if ( !(is_array($lineItemIds) && !empty($lineItemIds)) ) return;
  46.         $lineItemId $lineItemIds[0];
  47.         $salesChannelId $this->container->get(Connection::class)->fetchOne(
  48.             "SELECT LOWER(HEX(sales_channel_id)) FROM `order` WHERE id = (SELECT order_id FROM `order_line_item` WHERE id = UNHEX('{$lineItemId}') LIMIT 1) LIMIT 1"
  49.         );
  50.         if ( !$salesChannelId ) return;
  51.         if ( $this->salesChannelId === $salesChannelId ) return;
  52.         $this->salesChannelId $salesChannelId;
  53.         $this->salesChannel   $this->salesChannelRepository->search(new Criteria([$salesChannelId]), Context::createDefaultContext())->get($salesChannelId);
  54.         $this->pluginConfig   $this->systemConfigService->get('EnnoDigitalProducts.config'$salesChannelId);
  55.     }
  56.     public function onOrderLineItemLoaded(EntityLoadedEvent $event): void
  57.     {
  58.         $this->ennoInit($event);
  59.         if( !is_array($this->pluginConfig) ) return;
  60.         if( empty($this->pluginConfig) ) return;
  61.         if( !array_key_exists('active'$this->pluginConfig)) return;
  62.         if( !$this->pluginConfig["active"] ) return;
  63.         /** @var ProductEntity $productEntity */
  64.         foreach ($event->getEntities() as $entity)
  65.         {
  66.             if($entity->getType() !== LineItem::PRODUCT_LINE_ITEM_TYPE) continue;
  67.             $digitalData $this->digitalProductDataservice->getProductData($event->getContext(), $entity->getProductId());
  68.             if($digitalData$entity->addExtension('ennoDigitalProducts'$digitalData);
  69.         }
  70.     }
  71. }