custom/plugins/WbmTagManagerAnalytics/src/Subscriber/CartAddPrice/AfterLineItemAddedSubscriber.php line 44

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace Wbm\TagManagerAnalytics\Subscriber\CartAddPrice;
  4. use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
  5. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  6. use Shopware\Core\Checkout\Cart\Price\Struct\CalculatedPrice;
  7. use Shopware\Core\Content\Product\ProductEntity;
  8. use Shopware\Core\Framework\Context;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. use Symfony\Component\HttpFoundation\Session\SessionInterface;
  14. use Wbm\TagManagerAnalytics\Utility\SessionUtility;
  15. class AfterLineItemAddedSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var SessionInterface
  19.      */
  20.     protected $session;
  21.     /**
  22.      * @var EntityRepositoryInterface
  23.      */
  24.     protected $productRepository;
  25.     public function __construct(SessionInterface $sessionEntityRepositoryInterface $productRepository)
  26.     {
  27.         $this->session $session;
  28.         $this->productRepository $productRepository;
  29.     }
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             AfterLineItemAddedEvent::class => 'onAfterLineItemAdded'
  34.         ];
  35.     }
  36.     public function onAfterLineItemAdded(AfterLineItemAddedEvent $event)
  37.     {
  38.         if (!$this->session->has(SessionUtility::UPDATE_FLAG)
  39.             || $this->session->get(SessionUtility::UPDATE_FLAG) !== SessionUtility::ADDCART_UPDATEFLAG_VALUE) {
  40.             return;
  41.         }
  42.         $lineItems = [];
  43.         /** @var LineItem $addedLineItem */
  44.         foreach ($event->getLineItems() as $addedLineItem) {
  45.             $cartLineItem $event->getCart()->getLineItems()->get($addedLineItem->getId());
  46.             if (!$cartLineItem instanceof LineItem || !$cartLineItem->getPrice() instanceof CalculatedPrice) {
  47.                 continue;
  48.             }
  49.             if (
  50.                 !empty($cartLineItem->getPayload())
  51.                 && isset($cartLineItem->getPayload()['productNumber'])
  52.                 && !empty($cartLineItem->getPayload()['productNumber'])
  53.             ) {
  54.                 $lineItems[$cartLineItem->getPayload()['productNumber']] = $cartLineItem->getPrice()->getUnitPrice();
  55.                 continue;
  56.             }
  57.             // in case payload productNumber is not set, get the productNumber from DB product
  58.             $criteria = new Criteria();
  59.             $criteria->addFilter(new EqualsFilter('id'$addedLineItem->getId()));
  60.             $product $this->productRepository->search($criteria$event->getContext())->first();
  61.             if ($product instanceof ProductEntity) {
  62.                 $lineItems[$product->getProductNumber()] = $cartLineItem->getPrice()->getUnitPrice();
  63.             }
  64.         }
  65.         $this->session->set(SessionUtility::ADDCART_CART_ITEMS$lineItems);
  66.     }
  67. }