custom/plugins/BartCPreemiumArticle/src/Subscriber/AddToCart.php line 36

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace BartCPreemiumArticle\PremiumArticle\Subscriber;
  3. use BartCPreemiumArticle\PremiumArticle\Service\ProductHelper;
  4. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  5. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. use Symfony\Component\HttpFoundation\RequestStack;
  8. class AddToCart implements EventSubscriberInterface
  9. {
  10.     private ProductHelper $productHelper;
  11.     private CartService $cartService;
  12.     private RequestStack $requestStack;
  13.     public function __construct(
  14.         RequestStack $requestStack,
  15.         ProductHelper $productHelper,
  16.         CartService $cartService
  17.     ) {
  18.         $this->requestStack $requestStack;
  19.         $this->productHelper $productHelper;
  20.         $this->cartService $cartService;
  21.     }
  22.     public static function getSubscribedEvents(): array
  23.     {
  24.         return [
  25.             BeforeLineItemAddedEvent::class => 'whenLineItemAddToCart',
  26.         ];
  27.     }
  28.     public function whenLineItemAddToCart(BeforeLineItemAddedEvent $event): void
  29.     {
  30.         $request $this->requestStack->getCurrentRequest();
  31.         if (!$request) {
  32.             return;
  33.         }
  34.         $uri $request->getUri();
  35.         if (strpos($uri'/cart/add/id') === false && strpos($uri'/account/login') === false) {
  36.             $lineItemId $event->getLineItem()->getReferencedId();
  37.             if ($lineItemId) {
  38.                 $cart $event->getCart();
  39.                 $sellAvailable $this->productHelper->checkIfSellAvailable($lineItemId);
  40.                 if (!$sellAvailable) {
  41.                     $this->cartService->remove(
  42.                         $cart,
  43.                         $event->getLineItem()->getId(),
  44.                         $event->getSalesChannelContext()
  45.                     );
  46.                 }
  47.             }
  48.         }
  49.     }
  50. }