custom/plugins/Neon6Configurator/src/Subscribers/OnLineItemAdded.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Neon\Configurator\Subscribers;
  3. use Neon\Configurator\Exceptions\CalculationFormulaException;
  4. use Neon\Configurator\Neon6Configurator;
  5. use Neon\Configurator\Services\LineItemEnricher;
  6. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  7. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  8. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  9. use Symfony\Component\HttpFoundation\RequestStack;
  10. class OnLineItemAdded implements EventSubscriberInterface
  11. {
  12.     /** @var RequestStack */
  13.     private $requestStack;
  14.     private LineItemEnricher $lineItemEnricher;
  15.     public function __construct($requestStack$lineItemEnricher)
  16.     {
  17.         $this->requestStack $requestStack;
  18.         $this->lineItemEnricher $lineItemEnricher;
  19.     }
  20.     /**
  21.      * @inheritDoc
  22.      */
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             BeforeLineItemAddedEvent::class => 'onLineItemAdded'
  27.         ];
  28.     }
  29.     /**
  30.      * @param BeforeLineItemAddedEvent $event
  31.      * @throws CalculationFormulaException
  32.      */
  33.     public function onLineItemAdded(BeforeLineItemAddedEvent $event): void
  34.     {
  35.         $lineItem $event->getLineItem();
  36.         $isExistingProductAfterLogin $event->getLineItem()->getPayloadValue(Neon6Configurator::PAYLOAD_KEY) != null;
  37.         if (!$isExistingProductAfterLogin && $lineItem->getType() === LineItem::PRODUCT_LINE_ITEM_TYPE) {
  38.             $lineItemParam $this->requestStack->getCurrentRequest()->request->all('lineItems');
  39.             if ($lineItemParam != null && array_key_exists($lineItem->getId(), $lineItemParam)) {
  40.                 $this->lineItemEnricher->enrich($event->getCart(), $lineItem$lineItemParam[$lineItem->getId()], $event->getSalesChannelContext());
  41.             }
  42.         }
  43.     }
  44. }