vendor/shopware/core/Checkout/Cart/SalesChannel/CartItemAddRoute.php line 87

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Cart\SalesChannel;
  3. use OpenApi\Annotations as OA;
  4. use Shopware\Core\Checkout\Cart\Cart;
  5. use Shopware\Core\Checkout\Cart\CartCalculator;
  6. use Shopware\Core\Checkout\Cart\CartPersisterInterface;
  7. use Shopware\Core\Checkout\Cart\Event\AfterLineItemAddedEvent;
  8. use Shopware\Core\Checkout\Cart\Event\BeforeLineItemAddedEvent;
  9. use Shopware\Core\Checkout\Cart\Event\CartChangedEvent;
  10. use Shopware\Core\Checkout\Cart\LineItemFactoryRegistry;
  11. use Shopware\Core\Framework\Plugin\Exception\DecorationPatternException;
  12. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  13. use Shopware\Core\Framework\Routing\Annotation\Since;
  14. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  15. use Symfony\Component\HttpFoundation\Request;
  16. use Symfony\Component\Routing\Annotation\Route;
  17. use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
  18. /**
  19.  * @Route(defaults={"_routeScope"={"store-api"}})
  20.  */
  21. class CartItemAddRoute extends AbstractCartItemAddRoute
  22. {
  23.     /**
  24.      * @var CartCalculator
  25.      */
  26.     private $cartCalculator;
  27.     /**
  28.      * @var CartPersisterInterface
  29.      */
  30.     private $cartPersister;
  31.     /**
  32.      * @var EventDispatcherInterface
  33.      */
  34.     private $eventDispatcher;
  35.     /**
  36.      * @var LineItemFactoryRegistry
  37.      */
  38.     private $lineItemFactory;
  39.     /**
  40.      * @internal
  41.      */
  42.     public function __construct(
  43.         CartCalculator $cartCalculator,
  44.         CartPersisterInterface $cartPersister,
  45.         EventDispatcherInterface $eventDispatcher,
  46.         LineItemFactoryRegistry $lineItemFactory
  47.     ) {
  48.         $this->cartCalculator $cartCalculator;
  49.         $this->cartPersister $cartPersister;
  50.         $this->eventDispatcher $eventDispatcher;
  51.         $this->lineItemFactory $lineItemFactory;
  52.     }
  53.     public function getDecorated(): AbstractCartItemAddRoute
  54.     {
  55.         throw new DecorationPatternException(self::class);
  56.     }
  57.     /**
  58.      * @Since("6.3.0.0")
  59.      * @OA\Post(
  60.      *      path="/checkout/cart/line-item",
  61.      *      summary="Add items to the cart",
  62.      *      description="This route adds items to the cart. An item can be a product or promotion for example. They are referenced by the `referencedId`-parameter.
  63. Example: [Working with the cart - Guide](https://developer.shopware.com/docs/guides/integrations-api/store-api-guide/work-with-the-cart#adding-new-items-to-the-cart)",
  64.      *      operationId="addLineItem",
  65.      *      tags={"Store API", "Cart"},
  66.      *      @OA\RequestBody(
  67.      *          @OA\JsonContent(ref="#/components/schemas/CartItems")
  68.      *      ),
  69.      *      @OA\Response(
  70.      *          response="200",
  71.      *          description="The updated cart.",
  72.      *          @OA\JsonContent(ref="#/components/schemas/Cart")
  73.      *     )
  74.      * )
  75.      * @Route("/store-api/checkout/cart/line-item", name="store-api.checkout.cart.add", methods={"POST"})
  76.      */
  77.     public function add(Request $requestCart $cartSalesChannelContext $context, ?array $items): CartResponse
  78.     {
  79.         if ($items === null) {
  80.             $items = [];
  81.             /** @var array $item */
  82.             foreach ($request->request->all('items') as $item) {
  83.                 $items[] = $this->lineItemFactory->create($item$context);
  84.             }
  85.         }
  86.         foreach ($items as $item) {
  87.             $alreadyExists $cart->has($item->getId());
  88.             $cart->add($item);
  89.             $this->eventDispatcher->dispatch(new BeforeLineItemAddedEvent($item$cart$context$alreadyExists));
  90.         }
  91.         $cart->markModified();
  92.         $cart $this->cartCalculator->calculate($cart$context);
  93.         $this->cartPersister->save($cart$context);
  94.         $this->eventDispatcher->dispatch(new AfterLineItemAddedEvent($items$cart$context));
  95.         $this->eventDispatcher->dispatch(new CartChangedEvent($cart$context));
  96.         return new CartResponse($cart);
  97.     }
  98. }