custom/plugins/db_ProductConfigurator/src/Storefront/Page/Cart/CartItemSubscriber.php line 61

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace db_ProductConfigurator\Storefront\Page\Cart;
  3. use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
  4. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  5. use Shopware\Core\Checkout\Cart\Event\CartChangedEvent;
  6. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  7. use Shopware\Core\Checkout\Cart\Price\QuantityPriceCalculator;
  8. use Shopware\Core\Checkout\Cart\Price\Struct\QuantityPriceDefinition;
  9. use Shopware\Core\Checkout\Cart\SalesChannel\CartService;
  10. use Shopware\Core\Content\Product\Cart\ProductGatewayInterface;
  11. use Shopware\Core\Framework\Struct\ArrayStruct;
  12. use Shopware\Core\Framework\Uuid\Uuid;
  13. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  14. use Symfony\Component\HttpFoundation\RequestStack;
  15. class CartItemSubscriber implements EventSubscriberInterface
  16. {
  17.     /**
  18.      * @var RequestStack
  19.      */
  20.     private RequestStack $requestStack;
  21.     /**
  22.      * @var CartService
  23.      */
  24.     private $cartService;
  25.     /**
  26.      * @var QuantityPriceCalculator
  27.      */
  28.     private $calculator;
  29.     /**
  30.      * @var ProductGatewayInterface
  31.      */
  32.     private $productGateway;
  33.     public function __construct(
  34.         RequestStack $requestStack,
  35.         CartService $cartService,
  36.         QuantityPriceCalculator $calculator,
  37.         ProductGatewayInterface $productGateway
  38.     ) {
  39.         $this->requestStack $requestStack;
  40.         $this->cartService $cartService;
  41.         $this->calculator $calculator;
  42.         $this->productGateway $productGateway;
  43.     }
  44.     public static function getSubscribedEvents(): array
  45.     {
  46.         return [
  47.             BeforeLineItemAddedEvent::class => 'beforeLineItemAdded'
  48.         ];
  49.     }
  50.     public function beforeLineItemAdded(BeforeLineItemAddedEvent $event): void
  51.     {
  52.         $request $this->requestStack->getCurrentRequest();
  53.         $dbConfiguration $request->get('dbConfigurator');
  54.         $dbPrice $request->get('dbCalculatedPrice');
  55.         if ($dbConfiguration) {
  56.             $lineItem $event->getLineItem();
  57.             $cart $event->getCart();
  58.             $product $this->productGateway->get([$lineItem->getId()], $event->getSalesChannelContext());
  59.             $productId $lineItem->getId();
  60.             $dbQuantity =  $request->get('db-quantity');
  61.             if ($dbQuantity) {
  62.                 $quantity = (int)$dbQuantity;
  63.             } else {
  64.                 $quantity $cart->get($lineItem->getId())->getQuantity();
  65.             }
  66.             $cart->remove($lineItem->getId());
  67.             $newId Uuid::randomHex();
  68.             $newItem = new LineItem($newIdLineItem::PRODUCT_LINE_ITEM_TYPE$productId$quantity);
  69.             $newItem->setPayloadValue('dbPrice'$dbPrice);
  70.             $newItem->setPayloadValue('dbConfigurator'json_decode($dbConfigurationtrue))
  71.                 ->setStackable(true)
  72.                 ->setRemovable(true);
  73.             $cart->add($newItem);
  74.         }
  75.     }
  76. }