custom/plugins/NetiNextAccessManager/src/Subscriber/CartSubscriber.php line 48

Open in your IDE?
  1. <?php
  2. declare(strict_types=1);
  3. namespace NetInventors\NetiNextAccessManager\Subscriber;
  4. use NetInventors\NetiNextAccessManager\Components\Errors\Checkout\Cart\CustomerGroupBlockedForBuyError;
  5. use NetInventors\NetiNextAccessManager\Service\CustomerService;
  6. use NetInventors\NetiNextAccessManager\Service\PluginConfig;
  7. use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
  8. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  9. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  10. use Shopware\Core\Content\Product\ProductEntity;
  11. use Shopware\Core\Framework\Context;
  12. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  13. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. class CartSubscriber implements EventSubscriberInterface
  16. {
  17.     private PluginConfig              $pluginConfig;
  18.     private CustomerService           $customerService;
  19.     private CartService               $cartService;
  20.     private EntityRepositoryInterface $productRepository;
  21.     public function __construct(
  22.         PluginConfig              $pluginConfig,
  23.         CustomerService           $customerService,
  24.         CartService               $cartService,
  25.         EntityRepositoryInterface $productRepository
  26.     ) {
  27.         $this->pluginConfig      $pluginConfig;
  28.         $this->customerService   $customerService;
  29.         $this->productRepository $productRepository;
  30.         $this->cartService       $cartService;
  31.     }
  32.     public static function getSubscribedEvents(): array
  33.     {
  34.         return [
  35.             AfterLineItemAddedEvent::class => 'onAfterItemAdded',
  36.         ];
  37.     }
  38.     public function onAfterItemAdded(AfterLineItemAddedEvent $event): void
  39.     {
  40.         if (!$this->pluginConfig->isActive() || [] === $this->pluginConfig->getGroupsForBlockedBuyButton()) {
  41.             return;
  42.         }
  43.         $salesChannelContext $event->getSalesChannelContext();
  44.         if (!$this->customerService->isBuyButtonBlocked($salesChannelContext)) {
  45.             return;
  46.         }
  47.         $items $event->getLineItems();
  48.         $cart  $event->getCart();
  49.         $productNames $this->getProducts($items$event->getContext());
  50.         /** @var LineItem $item */
  51.         foreach ($items as $item) {
  52.             $itemId   $item->getId();
  53.             $itemName = (string) ($productNames[$itemId] ?? $itemId);
  54.             $this->cartService->remove($cart$itemId$salesChannelContext);
  55.             $cart->addErrors(
  56.                 new CustomerGroupBlockedForBuyError(
  57.                     $itemId,
  58.                     $itemName
  59.                 )
  60.             );
  61.         }
  62.     }
  63.     private function getProducts(array $itemsContext $context): array
  64.     {
  65.         $productNames = [];
  66.         $productIds   = [];
  67.         /** @var LineItem $item */
  68.         foreach ($items as $item) {
  69.             $productIds[] = [ 'id' => $item->getId() ];
  70.         }
  71.         $products $this->productRepository->search(new Criteria($productIds), $context)->getElements();
  72.         /** @var ProductEntity $product */
  73.         foreach ($products as $product) {
  74.             $productNames[$product->getId()] = $product->getName() ?? $product->getProductNumber();
  75.         }
  76.         return $productNames;
  77.     }
  78. }