vendor/store.shopware.com/netinexteasycoupon/src/Subscriber/LineItemSubscriber.php line 106

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextEasyCoupon\Subscriber;
  4. use NetInventors\NetiNextEasyCoupon\Core\Checkout\Cart\CartDataDefinition;
  5. use NetInventors\NetiNextEasyCoupon\Core\Content\Product\Cart\EasyCouponError;
  6. use NetInventors\NetiNextEasyCoupon\Service\VoucherRedemptionService;
  7. use NetInventors\NetiNextEasyCoupon\Struct\PluginConfigStruct;
  8. use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
  9. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  10. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemRemovedEvent;
  11. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  12. use Shopware\Core\Checkout\Promotion\Cart\PromotionItemBuilder;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. class LineItemSubscriber implements EventSubscriberInterface
  16. {
  17.     private PluginConfigStruct       $pluginConfig;
  18.     private VoucherRedemptionService $voucherRedemptionService;
  19.     private RequestStack             $requestStack;
  20.     private CartService              $cartService;
  21.     public function __construct(
  22.         VoucherRedemptionService $voucherRedemptionService,
  23.         PluginConfigStruct       $pluginConfig,
  24.         RequestStack             $requestStack,
  25.         CartService              $cartService
  26.     ) {
  27.         $this->voucherRedemptionService $voucherRedemptionService;
  28.         $this->pluginConfig             $pluginConfig;
  29.         $this->requestStack             $requestStack;
  30.         $this->cartService              $cartService;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             BeforeLineItemAddedEvent::class => [
  36.                 [ 'onLineItemAdded'5000 ],
  37.             ],
  38.             BeforeLineItemRemovedEvent::class => [
  39.                 [ 'onLineItemRemoved'5000 ],
  40.             ],
  41.             AfterLineItemAddedEvent::class => [
  42.                 [ 'onAfterLineItemAdded'5000 ],
  43.             ],
  44.         ];
  45.     }
  46.     /**
  47.      * @param AfterLineItemAddedEvent $event
  48.      */
  49.     public function onAfterLineItemAdded(AfterLineItemAddedEvent $event): void
  50.     {
  51.         if (!$this->pluginConfig->isActive()) {
  52.             return;
  53.         }
  54.         $cart           $event->getCart();
  55.         $vouchersInCart null;
  56.         /** @var CartDataDefinition $cartDataDefinition */
  57.         $cartDataDefinition $cart->getData()->get('easy-coupon-codes');
  58.         if (null !== $cartDataDefinition) {
  59.             $vouchersInCart $cartDataDefinition->getEasyCoupons();
  60.         }
  61.         $request $this->requestStack->getCurrentRequest();
  62.         if (null === $request) {
  63.             return;
  64.         }
  65.         $voucherCodes $request->getSession()->get('EasyCouponVoucherCodes');
  66.         if (null === $voucherCodes) {
  67.             return;
  68.         }
  69.         foreach ($voucherCodes as $code) {
  70.             if (isset($vouchersInCart[$code])) {
  71.                 continue;
  72.             }
  73.             $itemBuilder = new PromotionItemBuilder();
  74.             $lineItem $itemBuilder->buildPlaceholderItem($code);
  75.             $this->cartService->add($event->getCart(), $lineItem$event->getSalesChannelContext());
  76.         }
  77.     }
  78.     /**
  79.      * @param BeforeLineItemAddedEvent $event
  80.      *
  81.      * @throws EasyCouponError
  82.      */
  83.     public function onLineItemAdded(BeforeLineItemAddedEvent $event): void
  84.     {
  85.         if (!$this->pluginConfig->isActive()) {
  86.             return;
  87.         }
  88.         $this->voucherRedemptionService->addVoucherToCart($event);
  89.     }
  90.     public function onLineItemRemoved(BeforeLineItemRemovedEvent $event): void
  91.     {
  92.         if (!$this->pluginConfig->isActive()) {
  93.             return;
  94.         }
  95.         $this->voucherRedemptionService->removeVoucherFromCart($event->getCart(), $event->getLineItem());
  96.     }
  97. }