custom/plugins/SwagCustomizedProducts/src/Core/Checkout/Cart/Event/LineItemAddedSubscriber.php line 39

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. /*
  3.  * (c) shopware AG <info@shopware.com>
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Swag\CustomizedProducts\Core\Checkout\Cart\Event;
  8. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  9. use Shopware\Core\Content\Product\Cart\ProductNotFoundError;
  10. use Shopware\Core\Content\Product\ProductEntity;
  11. use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
  12. use Shopware\Core\System\SalesChannel\Entity\SalesChannelRepositoryInterface;
  13. use Swag\CustomizedProducts\Core\Checkout\CustomizedProductsCartDataCollector;
  14. use Swag\CustomizedProducts\Migration\Migration1565933910TemplateProduct;
  15. use Swag\CustomizedProducts\Storefront\Controller\CustomizedProductsCartController;
  16. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  17. class LineItemAddedSubscriber implements EventSubscriberInterface
  18. {
  19.     /**
  20.      * @var SalesChannelRepositoryInterface
  21.      */
  22.     private $productRepository;
  23.     public function __construct(SalesChannelRepositoryInterface $productRepository)
  24.     {
  25.         $this->productRepository $productRepository;
  26.     }
  27.     public static function getSubscribedEvents(): array
  28.     {
  29.         return [
  30.             BeforeLineItemAddedEvent::class => 'onLineItemAddedToCart',
  31.         ];
  32.     }
  33.     public function onLineItemAddedToCart(BeforeLineItemAddedEvent $event): void
  34.     {
  35.         $lineItem $event->getLineItem();
  36.         if ($lineItem->getType() !== CustomizedProductsCartDataCollector::CUSTOMIZED_PRODUCTS_TEMPLATE_LINE_ITEM_TYPE) {
  37.             return;
  38.         }
  39.         if ($lineItem->hasExtension(CustomizedProductsCartController::ADD_TO_CART_IDENTIFIER)) {
  40.             return;
  41.         }
  42.         $referencedId $lineItem->getReferencedId();
  43.         if ($referencedId === null || $referencedId === '') {
  44.             return;
  45.         }
  46.         $criteria = new Criteria([$referencedId]);
  47.         $criteria->addAssociation(Migration1565933910TemplateProduct::PRODUCT_TEMPLATE_INHERITANCE_COLUMN);
  48.         /** @var ProductEntity|null $product */
  49.         $product $this->productRepository->search($criteria$event->getSalesChannelContext())->first();
  50.         if ($product === null || !$product->hasExtension(Migration1565933910TemplateProduct::PRODUCT_TEMPLATE_INHERITANCE_COLUMN)) {
  51.             return;
  52.         }
  53.         // Custom product is added by order number and has to be removed
  54.         $event->getCart()->remove($lineItem->getId());
  55.         $event->getCart()->getErrors()->add(new ProductNotFoundError($referencedId));
  56.     }
  57. }