custom/plugins/CioSponsoredOrderConfirmation/src/Subscriber/AddToCartSubscriber.php line 42

Open in your IDE?
  1. <?php
  2. namespace CioSponsoredOrderConfirmation\Subscriber;
  3. use CioProductCustomerInputs\Service\SessionService;
  4. use Shopware\Core\Checkout\Cart\Error\IncompleteLineItemError;
  5. use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
  6. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  7. use Shopware\Core\Checkout\Cart\Event\CartChangedEvent;
  8. use Shopware\Core\Content\Product\ProductEntity;
  9. use Shopware\Core\Framework\DataAbstractionLayer\EntityRepositoryInterface;
  10. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
  12. use Shopware\Storefront\Framework\Routing\Router;
  13. use Symfony\Component\DependencyInjection\ContainerInterface;
  14. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  15. use Symfony\Component\HttpFoundation\RedirectResponse;
  16. use Symfony\Component\HttpFoundation\RequestStack;
  17. class AddToCartSubscriber implements EventSubscriberInterface
  18. {
  19.     private ContainerInterface $container;
  20.     private Router $router;
  21.     private RequestStack $requestStack;
  22.     private SessionService $sessionService;
  23.     public function __construct(ContainerInterface $containerRouter $routerRequestStack $requestStackSessionService $sessionService)
  24.     {
  25.         $this->container $container;
  26.         $this->router $router;
  27.         $this->requestStack $requestStack;
  28.         $this->sessionService $sessionService;
  29.     }
  30.     public static function getSubscribedEvents()
  31.     {
  32.         return [
  33.             BeforeLineItemAddedEvent::class => 'onBeforeLineItemAddedEvent'
  34.         ];
  35.     }
  36.     public function onBeforeLineItemAddedEvent(BeforeLineItemAddedEvent $event)
  37.     {
  38.         // Handle only product line items to avoid invalid referencedId filters (e.g. "frontend.cart.offcanvas")
  39.         if ($event->getLineItem()->getType() !== \Shopware\Core\Checkout\Cart\LineItem\LineItem::PRODUCT_LINE_ITEM_TYPE) {
  40.             return;
  41.         }
  42.         $lineItem = clone $event->getLineItem();
  43.         /** @var EntityRepositoryInterface $productRepository */
  44.         $productRepository $this->container->get('product.repository');
  45.         $criteria = (new Criteria())->addFilter(new EqualsFilter('id'$lineItem->getReferencedId()));
  46.         /** @var ProductEntity|null $product */
  47.         $product $productRepository->search($criteria$event->getContext())->first();
  48.         if (!is_null($product) && is_array($product->getCustomFields())) {
  49.             $numberOfRows $this->getCustomFieldValue($product'cio_customer_input_count_rows'0);
  50.             $numberOfCells count($this->getCustomFieldValue($product'cio_customer_input_general_group', []));
  51.             for ($row 1$row <= $numberOfRows$row++) {
  52.                 for ($cell 1$cell <= $numberOfCells$cell++) {
  53.                     $error false;
  54.                     $value $this->sessionService->getCustomerInputFromSession($product->getProductNumber(), 'value'$row$cell);
  55.                     // 0 is a valid value
  56.                     if (!is_numeric($value)) {
  57.                         if (empty($value)) {
  58.                             $error true;
  59.                         }
  60.                     }
  61.                     if ($error) {
  62.                         if ($event->getCart()->has($event->getLineItem()->getId())) {
  63.                             $event->getCart()->remove($event->getLineItem()->getId());
  64.                         }
  65.                         $event->getCart()->addErrors(new IncompleteLineItemError($event->getLineItem()->getReferencedId(), 'cioCustomInputError'));
  66.                         if ($this->requestStack->getCurrentRequest()->request->has('cioAlternativeRedirectTo')) {
  67.                             (new RedirectResponse(
  68.                                 $this->router->generate('frontend.detail.page', ['productId' => $event->getLineItem()->getId()])
  69.                             ))->send();
  70.                         }
  71.                     }
  72.                 }
  73.             }
  74.         }
  75.     }
  76.     protected function getCustomFieldValue(ProductEntity $productEntity$key$default null)
  77.     {
  78.         if (!is_array($productEntity->getCustomFields()) || !key_exists($key$productEntity->getCustomFields())) {
  79.             return $default;
  80.         }
  81.         return $productEntity->getCustomFields()[$key];
  82.     }
  83. }