vendor/store.shopware.com/ennodigitalproducts/Subscriber/LineItemAdded.php line 76

Open in your IDE?
  1. <?php
  2. namespace EnnoDigitalProducts\Subscriber;
  3. use EnnoDigitalProducts\Service\DigitalProductDataService;
  4. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  5. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  6. use Shopware\Core\Checkout\Order\OrderEvents;
  7. use Shopware\Core\Content\Product\ProductEntity;
  8. use Shopware\Core\Content\Product\ProductEvents;
  9. use Shopware\Core\Framework\DataAbstractionLayer\Event\EntityLoadedEvent;
  10. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Sorting\FieldSorting;
  14. use Shopware\Core\Framework\Context;
  15. use Shopware\Core\System\SystemConfig\SystemConfigService;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class LineItemAdded implements EventSubscriberInterface
  18. {
  19.     private $systemConfigService;
  20.     private $digitalProductDataservice;
  21.     private $salesChannelRepository;
  22.     private $container;
  23.     private $salesChannelId;
  24.     private $salesChannel;
  25.     private $pluginConfig;
  26.     public function __construct(SystemConfigService $systemConfigServiceDigitalProductDataService $digitalProductDataserviceEntityRepositoryInterface $salesChannelRepository$container)
  27.     {
  28.         $this->systemConfigService $systemConfigService;
  29.         $this->digitalProductDataservice $digitalProductDataservice;
  30.         $this->salesChannelRepository $salesChannelRepository;
  31.         $this->container $container;
  32.     }
  33.     public static function getSubscribedEvents()
  34.     {
  35.         return [
  36.             BeforeLineItemAddedEvent::class => 'onLineItemAdded'
  37.         ];
  38.     }
  39.     private function ennoInit()
  40.     {
  41.         $salesChannelId null;
  42.         try
  43.         {
  44.             $requestStack $this->container->get('request_stack');
  45.             if ( $requestStack && method_exists($requestStack'getCurrentRequest') )
  46.             {
  47.                 $currentRequest $requestStack->getCurrentRequest();
  48.                 if ($currentRequest && method_exists($currentRequest'get') )
  49.                 {
  50.                     $salesChannelId $currentRequest->get("sw-sales-channel-id");
  51.                 }
  52.             }
  53.         }
  54.         catch (\Exception $e) { }
  55.         if( !$salesChannelId ) return;
  56.         if( $this->salesChannelId === $salesChannelId ) return;
  57.         $this->salesChannelId $salesChannelId;
  58.         $this->salesChannel   $this->salesChannelRepository->search(new Criteria([$salesChannelId]), Context::createDefaultContext())->get($salesChannelId);
  59.         $this->pluginConfig   $this->systemConfigService->get('EnnoDigitalProducts.config'$salesChannelId);
  60.     }
  61.     public function onLineItemAdded(BeforeLineItemAddedEvent $event)
  62.     {
  63.         $this->ennoInit();
  64.         if( !is_array($this->pluginConfig) ) return;
  65.         if( empty($this->pluginConfig) ) return;
  66.         if( !array_key_exists('active'$this->pluginConfig)) return;
  67.         if( !$this->pluginConfig["active"] ) return;
  68.         $lineItem $event->getLineItem();
  69.         if($lineItem->getType() !== LineItem::PRODUCT_LINE_ITEM_TYPE) return;
  70.         $digitalData $this->digitalProductDataservice->getProductData(Context::createDefaultContext(), $lineItem->getId(), false, ['settings'])->getVars();
  71.         if(is_array($digitalData) && is_array($digitalData['settings']))
  72.         {
  73.             $lineItem->setPayloadValue('ennoIsDigital'$digitalData['settings']['isDigital']);
  74.         }
  75.     }
  76. }