custom/plugins/GbmedEnev/src/Subscriber/LineItemAdded.php line 53

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * gb media
  4.  * All Rights Reserved.
  5.  *
  6.  * Unauthorized copying of this file, via any medium is strictly prohibited.
  7.  * The content of this file is proprietary and confidential.
  8.  *
  9.  * @category       Shopware
  10.  * @package        Shopware_Plugins
  11.  * @subpackage     GbmedEnev
  12.  * @copyright      Copyright (c) 2019, gb media
  13.  * @license        proprietary
  14.  * @author         Giuseppe Bottino
  15.  * @link           http://www.gb-media.biz
  16.  */
  17. namespace Gbmed\Enev\Subscriber;
  18. use Exception;
  19. use Gbmed\Enev\Core\Content\Enev\EnevDefinition;
  20. use Gbmed\Enev\Service\Product as ProductService;
  21. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  22. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  23. use Shopware\Core\Framework\DataAbstractionLayer\EntityCollection;
  24. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  25. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  26. use Symfony\Component\HttpFoundation\Request;
  27. class LineItemAdded implements EventSubscriberInterface
  28. {
  29.     /** @var ProductService */
  30.     private $productService;
  31.     /**
  32.      * Product constructor.
  33.      *
  34.      * @param ProductService $productService
  35.      */
  36.     public function __construct(
  37.         ProductService $productService
  38.     ) {
  39.         $this->productService $productService;
  40.     }
  41.     public static function getSubscribedEvents()
  42.     {
  43.         return [
  44.             BeforeLineItemAddedEvent::class => 'onBeforeLineItemAddedEvent',
  45.         ];
  46.     }
  47.     public function onBeforeLineItemAddedEvent(BeforeLineItemAddedEvent $event)
  48.     {
  49.         $request Request::createFromGlobals();
  50.         $requestLineItems $request->request->get('lineItems');
  51.         /** @var LineItem $lineItem */
  52.         foreach ($event->getCart()->getLineItems() as $key => $lineItem) {
  53.             if (empty($requestLineItems[$lineItem->getId()]) || $lineItem->hasExtension(EnevDefinition::propertyName())) {
  54.                 continue;
  55.             }
  56.             $result $this->findEnevByProduct($lineItem$event->getSalesChannelContext());
  57.             $lineItem->addExtension(EnevDefinition::propertyName(), $result);
  58.             $lineItem->markModified();
  59.             $event->getCart()->markModified();
  60.         }
  61.     }
  62.     /**
  63.      * helper to find enev
  64.      *
  65.      * @param LineItem $lineItem
  66.      * @param SalesChannelContext $salesChannelContext
  67.      * @return EntityCollection|null
  68.      * @throws \GuzzleHttp\Exception\GuzzleException
  69.      */
  70.     private function findEnevByProduct(
  71.         LineItem $lineItem,
  72.         SalesChannelContext $salesChannelContext
  73.     ): ?EntityCollection {
  74.         try {
  75.             $result $this->productService->findEnevByProductId(
  76.                 $lineItem->getId(),
  77.                 $salesChannelContext->getContext()
  78.             );
  79.             $collection $result->getTotal()
  80.                 ? $this->productService->prepareEprel($result->getEntities(), $salesChannelContext)
  81.                 : null;
  82.         } catch (Exception $e) {
  83.             $collection null;
  84.         }
  85.         return $collection;
  86.     }
  87. }