vendor/shopware/core/Checkout/Cart/SalesChannel/CartService.php line 92

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Checkout\Cart\SalesChannel;
  3. use Shopware\Core\Checkout\Cart\Cart;
  4. use Shopware\Core\Checkout\Cart\CartCalculator;
  5. use Shopware\Core\Checkout\Cart\CartPersisterInterface;
  6. use Shopware\Core\Checkout\Cart\Event\CartChangedEvent;
  7. use Shopware\Core\Checkout\Cart\Event\CartCreatedEvent;
  8. use Shopware\Core\Checkout\Cart\Exception\InvalidQuantityException;
  9. use Shopware\Core\Checkout\Cart\Exception\LineItemNotFoundException;
  10. use Shopware\Core\Checkout\Cart\Exception\LineItemNotRemovableException;
  11. use Shopware\Core\Checkout\Cart\Exception\LineItemNotStackableException;
  12. use Shopware\Core\Checkout\Cart\Exception\MixedLineItemTypeException;
  13. use Shopware\Core\Checkout\Cart\LineItem\LineItem;
  14. use Shopware\Core\Checkout\Payment\Exception\InvalidOrderException;
  15. use Shopware\Core\Framework\DataAbstractionLayer\Exception\InconsistentCriteriaIdsException;
  16. use Shopware\Core\Framework\Log\Package;
  17. use Shopware\Core\Framework\Validation\DataBag\RequestDataBag;
  18. use Shopware\Core\System\SalesChannel\SalesChannelContext;
  19. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  20. use Symfony\Component\HttpFoundation\Request;
  21. use Symfony\Contracts\Service\ResetInterface;
  22. #[Package('checkout')]
  23. class CartService implements ResetInterface
  24. {
  25.     public const SALES_CHANNEL 'sales-channel';
  26.     /**
  27.      * @var Cart[]
  28.      */
  29.     private $cart = [];
  30.     private EventDispatcherInterface $eventDispatcher;
  31.     private AbstractCartLoadRoute $loadRoute;
  32.     private AbstractCartDeleteRoute $deleteRoute;
  33.     private CartCalculator $calculator;
  34.     private AbstractCartItemUpdateRoute $itemUpdateRoute;
  35.     private AbstractCartItemRemoveRoute $itemRemoveRoute;
  36.     private AbstractCartItemAddRoute $itemAddRoute;
  37.     private AbstractCartOrderRoute $orderRoute;
  38.     private CartPersisterInterface $persister;
  39.     /**
  40.      * @internal
  41.      */
  42.     public function __construct(
  43.         CartPersisterInterface $persister,
  44.         EventDispatcherInterface $eventDispatcher,
  45.         CartCalculator $calculator,
  46.         AbstractCartLoadRoute $loadRoute,
  47.         AbstractCartDeleteRoute $deleteRoute,
  48.         AbstractCartItemAddRoute $itemAddRoute,
  49.         AbstractCartItemUpdateRoute $itemUpdateRoute,
  50.         AbstractCartItemRemoveRoute $itemRemoveRoute,
  51.         AbstractCartOrderRoute $orderRoute
  52.     ) {
  53.         $this->persister $persister;
  54.         $this->eventDispatcher $eventDispatcher;
  55.         $this->loadRoute $loadRoute;
  56.         $this->deleteRoute $deleteRoute;
  57.         $this->calculator $calculator;
  58.         $this->itemUpdateRoute $itemUpdateRoute;
  59.         $this->itemRemoveRoute $itemRemoveRoute;
  60.         $this->itemAddRoute $itemAddRoute;
  61.         $this->orderRoute $orderRoute;
  62.     }
  63.     public function setCart(Cart $cart): void
  64.     {
  65.         $this->cart[$cart->getToken()] = $cart;
  66.     }
  67.     public function createNew(string $tokenstring $name self::SALES_CHANNEL): Cart
  68.     {
  69.         $cart = new Cart($name$token);
  70.         $this->eventDispatcher->dispatch(new CartCreatedEvent($cart));
  71.         return $this->cart[$cart->getToken()] = $cart;
  72.     }
  73.     public function getCart(
  74.         string $token,
  75.         SalesChannelContext $context,
  76.         string $name self::SALES_CHANNEL,
  77.         bool $caching true
  78.     ): Cart {
  79.         if ($caching && isset($this->cart[$token])) {
  80.             return $this->cart[$token];
  81.         }
  82.         $request = new Request();
  83.         $request->query->set('name'$name);
  84.         $request->query->set('token'$token);
  85.         $cart $this->loadRoute->load($request$context)->getCart();
  86.         return $this->cart[$cart->getToken()] = $cart;
  87.     }
  88.     /**
  89.      * @param LineItem|LineItem[] $items
  90.      *
  91.      * @throws InvalidQuantityException
  92.      * @throws LineItemNotStackableException
  93.      * @throws MixedLineItemTypeException
  94.      */
  95.     public function add(Cart $cart$itemsSalesChannelContext $context): Cart
  96.     {
  97.         if ($items instanceof LineItem) {
  98.             $items = [$items];
  99.         }
  100.         $cart $this->itemAddRoute->add(new Request(), $cart$context$items)->getCart();
  101.         return $this->cart[$cart->getToken()] = $cart;
  102.     }
  103.     /**
  104.      * @throws LineItemNotFoundException
  105.      * @throws LineItemNotStackableException
  106.      * @throws InvalidQuantityException
  107.      */
  108.     public function changeQuantity(Cart $cartstring $identifierint $quantitySalesChannelContext $context): Cart
  109.     {
  110.         $request = new Request();
  111.         $request->request->set('items', [
  112.             [
  113.                 'id' => $identifier,
  114.                 'quantity' => $quantity,
  115.             ],
  116.         ]);
  117.         $cart $this->itemUpdateRoute->change($request$cart$context)->getCart();
  118.         return $this->cart[$cart->getToken()] = $cart;
  119.     }
  120.     /**
  121.      * @throws LineItemNotFoundException
  122.      * @throws LineItemNotRemovableException
  123.      */
  124.     public function remove(Cart $cartstring $identifierSalesChannelContext $context): Cart
  125.     {
  126.         $request = new Request();
  127.         $request->request->set('ids', [$identifier]);
  128.         $cart $this->itemRemoveRoute->remove($request$cart$context)->getCart();
  129.         return $this->cart[$cart->getToken()] = $cart;
  130.     }
  131.     /**
  132.      * @throws InvalidOrderException
  133.      * @throws InconsistentCriteriaIdsException
  134.      */
  135.     public function order(Cart $cartSalesChannelContext $contextRequestDataBag $data): string
  136.     {
  137.         $orderId $this->orderRoute->order($cart$context$data)->getOrder()->getId();
  138.         if (isset($this->cart[$cart->getToken()])) {
  139.             unset($this->cart[$cart->getToken()]);
  140.         }
  141.         $cart $this->createNew($context->getToken(), $cart->getName());
  142.         $this->eventDispatcher->dispatch(new CartChangedEvent($cart$context));
  143.         return $orderId;
  144.     }
  145.     public function recalculate(Cart $cartSalesChannelContext $context): Cart
  146.     {
  147.         $cart $this->calculator->calculate($cart$context);
  148.         $this->persister->save($cart$context);
  149.         return $cart;
  150.     }
  151.     public function deleteCart(SalesChannelContext $context): void
  152.     {
  153.         $this->deleteRoute->delete($context);
  154.     }
  155.     public function reset(): void
  156.     {
  157.         $this->cart = [];
  158.     }
  159. }