vendor/crehler/mojebambino-engine/src/Subscriber/LineItemAddSubscriber.php line 59

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /**
  3.  * @copyright 2020 Crehler Sp. z o. o. <https://crehler.com>
  4.  *
  5.  * @author    Jakub MedyƄski <jme@crehler.com>
  6.  * @support   Crehler <support@crehler.com>
  7.  * @created   03 lip 2020
  8.  *
  9.  * For the full copyright and license information, please view the LICENSE
  10.  * file that was distributed with this source code.
  11.  */
  12. namespace Crehler\MojeBambinoEngine\Subscriber;
  13. use Shopware\Core\Checkout\Cart\Error\ErrorCollection;
  14. use Shopware\Core\Checkout\Cart\Error\IncompleteLineItemError;
  15. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  16. use Shopware\Core\Checkout\Cart\Event\LineItemAddedEvent;
  17. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  18. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
  19. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  20. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  21. use Shopware\Core\Framework\Uuid\Uuid;
  22. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  23. use Symfony\Component\HttpFoundation\Session\Session;
  24. class LineItemAddSubscriber implements EventSubscriberInterface
  25. {
  26.     /**
  27.      * @var EntityRepositoryInterface
  28.      */
  29.     protected $productRepository;
  30.     /**
  31.      * @var Session
  32.      */
  33.     protected $session;
  34.     protected $translator;
  35.     public function __construct(
  36.         EntityRepository $exampleRepository,
  37.         Session          $session,
  38.                          $translator
  39.     )
  40.     {
  41.         $this->productRepository $exampleRepository;
  42.         $this->session $session;
  43.         $this->translator $translator;
  44.     }
  45.     public static function getSubscribedEvents(): array
  46.     {
  47.         return [
  48.             BeforeLineItemAddedEvent::class => 'onLineItemAdded'
  49.         ];
  50.     }
  51.     public function onLineItemAdded(BeforeLineItemAddedEvent $event): void
  52.     {
  53.         $lineItem $event->getLineItem();
  54.         $cart $event->getCart();
  55.         $context $event->getContext();
  56.         if ($lineItem->getType() !== LineItem::PRODUCT_LINE_ITEM_TYPE) {
  57.             return;
  58.         }
  59.         $id $lineItem->getReferencedId();
  60.         if (!is_string($id) || !Uuid::isValid($id)) {
  61.             return;
  62.         }
  63.         $product $this->productRepository->search(new Criteria([$id]), $context)->getEntities()->first();
  64.         if ($product === null) {
  65.             return;
  66.         }
  67.         $customFields $product->getCustomFields();
  68.         if (!is_array($customFields)) {
  69.             return;
  70.         }
  71.         if (
  72.             isset($customFields['crehler_product_exclude_from_cart'])
  73.             && $customFields['crehler_product_exclude_from_cart'] === true
  74.         ) {
  75.             $errorCollection = new ErrorCollection();
  76.             $errorCollection->add(new IncompleteLineItemError($lineItem->getId(), $this->translator->trans("bambino.checkout.unbuyableProduct")));
  77.             $cart->remove($lineItem->getId());
  78.             $cart->setErrors($errorCollection);
  79.         }
  80.     }
  81. }