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

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