custom/plugins/BartCPreemiumArticle/src/Subscriber/BeforeRemoveItemFormCart.php line 29

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BartCPreemiumArticle\PremiumArticle\Subscriber;
  4. use BartCPreemiumArticle\PremiumArticle\Core\Content\Premium\PremiumEntity;
  5. use BartCPreemiumArticle\PremiumArticle\Service\CartHelper;
  6. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemRemovedEvent;
  7. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  8. class BeforeRemoveItemFormCart implements EventSubscriberInterface
  9. {
  10.     private CartHelper $cartHelper;
  11.     public function __construct(CartHelper $cartHelper)
  12.     {
  13.         $this->cartHelper $cartHelper;
  14.     }
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             BeforeLineItemRemovedEvent::class => 'onBeforeLineItemRemovedEvent',
  19.         ];
  20.     }
  21.     // todo czy ta klasa jest potrzebna? Nie robi tego Processor?
  22.     public function onBeforeLineItemRemovedEvent(BeforeLineItemRemovedEvent $event): void
  23.     {
  24.         $cart $event->getCart();
  25.         $productItems $cart->getLineItems()->filterType('product');
  26.         $regularProductInCart false;
  27.         foreach ($productItems as $productItem) {
  28.             $premiumProductEntity $productItem->getExtension('premiumProductEntity');
  29.             if (!($premiumProductEntity instanceof PremiumEntity)) {
  30.                 $regularProductInCart true;
  31.                 break;
  32.             }
  33.         }
  34.         if (!$regularProductInCart) {
  35.             $this->cartHelper->removePremiumItems($cart$event->getSalesChannelContext());
  36.         }
  37.     }
  38. }