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

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace BartCPreemiumArticle\PremiumArticle\Subscriber;
  4. use BartCPreemiumArticle\PremiumArticle\Service\ProductHelper;
  5. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  6. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  7. class RemovePreemiumArticleFromCart implements EventSubscriberInterface
  8. {
  9.     private ProductHelper $checkIfPremiumArticleSell;
  10.     public function __construct(
  11.         ProductHelper $checkIfPremiumArticleSell
  12.     ) {
  13.         $this->checkIfPremiumArticleSell $checkIfPremiumArticleSell;
  14.     }
  15.     public static function getSubscribedEvents(): array
  16.     {
  17.         return [
  18.             BeforeLineItemAddedEvent::class => 'onProductAdded',
  19.         ];
  20.     }
  21.     //todo Czy to się kiedyś wydarzy? Klasa raczej do usunięcia
  22.     public function onProductAdded(BeforeLineItemAddedEvent $event): void
  23.     {
  24.         $cart $event->getCart();
  25.         $product $event->getLineItem();
  26.         if (strpos($product->getId(), 'premiumarticle') === false && $product->getType() === 'product') {
  27.             $sell $this->checkIfPremiumArticleSell->checkIfSellAvailable($product->getId());
  28.             if (!$sell && $cart->has($product->getId())) {
  29.                 $extension $product->getExtension('view');
  30.                 if ($extension === null || !isset($extension->getVars()['data']['premiumProduct'])
  31.                     || $extension->getVars()['data']['premiumProduct'] === false) {
  32.                     $cart->remove($product->getId());
  33.                 }
  34.             }
  35.         }
  36.     }
  37. }