custom/plugins/NxsCoreFixes/src/Communication/Subscriber/CartSubscriber.php line 59

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Nxs\NxsCoreFixes\Communication\Subscriber;
  3. use Shopware\Core\Checkout\Cart\Cart;
  4. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  5. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  6. use Shopware\Core\Checkout\Promotion\Cart\Error\PromotionNotFoundError;
  7. use Shopware\Core\Checkout\Promotion\Cart\Extension\CartExtension;
  8. use Shopware\Storefront\Page\Checkout\Cart\CheckoutCartPageLoadedEvent;
  9. use Shopware\Storefront\Page\Checkout\Confirm\CheckoutConfirmPageLoadedEvent;
  10. use Shopware\Storefront\Page\Checkout\Offcanvas\OffcanvasCartPageLoadedEvent;
  11. use Shopware\Storefront\Page\PageLoadedEvent;
  12. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  13. class CartSubscriber implements EventSubscriberInterface
  14. {
  15.     /**
  16.      * @var $cartService
  17.      */
  18.     private $cartService;
  19.     public function __construct(CartService $cartService)
  20.     {
  21.         $this->cartService $cartService;
  22.     }
  23.     public static function getSubscribedEvents()
  24.     {
  25.         return [
  26.             OffcanvasCartPageLoadedEvent::class => 'onOffcanvasCartPageLoaded',
  27.             CheckoutCartPageLoadedEvent::class => 'onCartPageLoaded',
  28.             CheckoutConfirmPageLoadedEvent::class => 'onConfirmPageLoaded'
  29.         ];
  30.     }
  31.     public function onOffcanvasCartPageLoaded(OffcanvasCartPageLoadedEvent $event)
  32.     {
  33.         $page $event->getPage();
  34.         $lineItems $page->getCart()->getLineItems();
  35.         $promotions 0;
  36.         foreach ($lineItems as $lineItem)
  37.         {
  38.             if($lineItem->getType() === LineItem::PROMOTION_LINE_ITEM_TYPE)
  39.             {
  40.                 $promotions++;
  41.             }
  42.         }
  43.         $productCount count($lineItems) - $promotions;
  44.         $page->assign(['promotionCount' => $promotions'productCount' => $productCount]);
  45.     }
  46.     /**
  47.      * @param CheckoutCartPageLoadedEvent $event
  48.      */
  49.     public function onCartPageLoaded(CheckoutCartPageLoadedEvent $event):void
  50.     {
  51.         $this->removeUnmatchingPromotionCode($event);
  52.     }
  53.     /**
  54.      * @param CheckoutConfirmPageLoadedEvent $event
  55.      */
  56.     public function onConfirmPageLoaded(CheckoutConfirmPageLoadedEvent $event): void
  57.     {
  58.         $this->removeUnmatchingPromotionCode($event);
  59.     }
  60.     private function getExtension(Cart $cart): CartExtension
  61.     {
  62.         if (!$cart->hasExtension(CartExtension::KEY)) {
  63.             $cart->addExtension(CartExtension::KEY, new CartExtension());
  64.         }
  65.         /** @var CartExtension $extension */
  66.         $extension $cart->getExtension(CartExtension::KEY);
  67.         return $extension;
  68.     }
  69.     /**
  70.      * @param PageLoadedEvent $event
  71.      */
  72.     private function removeUnmatchingPromotionCode(PageLoadedEvent $event): void
  73.     {
  74.         $page $event->getPage();
  75.         $salesChannelContext $event->getSalesChannelContext();
  76.         /** @var Cart $cart */
  77.         $cart $page->getCart();
  78.         $errorFound false;
  79.         $errors $cart->getErrors();
  80.         foreach ($errors as $error) {
  81.             if ($error instanceof PromotionNotFoundError) {
  82.                 $code $error->getCode();
  83.                 $extension $this->getExtension($cart);
  84.                 $extension->removeCode($code);
  85.                 $errors->remove($error->getId());
  86.                 $cart->addExtension(CartExtension::KEY$extension);
  87.                 $errorFound true;
  88.             }
  89.         }
  90.         if($errorFound)
  91.         {
  92.             $cart->setErrors($errors);
  93.             $this->cartService->setCart($cart);
  94.             $this->cartService->recalculate($cart$salesChannelContext);
  95.             $page->setCart($cart);
  96.         }
  97.     }
  98. }