vendor/pimcore/pimcore/bundles/EcommerceFrameworkBundle/CartManager/MultiCartManager.php line 338

Open in your IDE?
  1. <?php
  2. /**
  3.  * Pimcore
  4.  *
  5.  * This source file is available under two different licenses:
  6.  * - GNU General Public License version 3 (GPLv3)
  7.  * - Pimcore Commercial License (PCL)
  8.  * Full copyright and license information is available in
  9.  * LICENSE.md which is distributed with this source code.
  10.  *
  11.  *  @copyright  Copyright (c) Pimcore GmbH (http://www.pimcore.org)
  12.  *  @license    http://www.pimcore.org/license     GPLv3 and PCL
  13.  */
  14. namespace Pimcore\Bundle\EcommerceFrameworkBundle\CartManager;
  15. use Pimcore\Bundle\EcommerceFrameworkBundle\CheckoutManager\V7\CheckoutManager;
  16. use Pimcore\Bundle\EcommerceFrameworkBundle\EnvironmentInterface;
  17. use Pimcore\Bundle\EcommerceFrameworkBundle\Exception\InvalidConfigException;
  18. use Pimcore\Bundle\EcommerceFrameworkBundle\Model\CheckoutableInterface;
  19. use Pimcore\Bundle\EcommerceFrameworkBundle\OrderManager\OrderManagerLocatorInterface;
  20. use Psr\Log\LoggerInterface;
  21. class MultiCartManager implements CartManagerInterface
  22. {
  23.     /**
  24.      * @var EnvironmentInterface
  25.      */
  26.     protected $environment;
  27.     /**
  28.      * @var CartFactoryInterface
  29.      */
  30.     protected $cartFactory;
  31.     /**
  32.      * @var CartPriceCalculatorFactoryInterface
  33.      */
  34.     protected $cartPriceCalculatorFactory;
  35.     /**
  36.      * @var OrderManagerLocatorInterface
  37.      */
  38.     protected $orderManagers;
  39.     /**
  40.      * @var LoggerInterface
  41.      */
  42.     protected $logger;
  43.     /**
  44.      * @var CartInterface[]
  45.      */
  46.     protected $carts = [];
  47.     /**
  48.      * @var bool
  49.      */
  50.     protected $initialized false;
  51.     /**
  52.      * @param EnvironmentInterface $environment
  53.      * @param CartFactoryInterface $cartFactory
  54.      * @param CartPriceCalculatorFactoryInterface $cartPriceCalculatorFactory
  55.      * @param OrderManagerLocatorInterface $orderManagers
  56.      * @param LoggerInterface $logger
  57.      */
  58.     public function __construct(
  59.         EnvironmentInterface $environment,
  60.         CartFactoryInterface $cartFactory,
  61.         CartPriceCalculatorFactoryInterface $cartPriceCalculatorFactory,
  62.         OrderManagerLocatorInterface $orderManagers,
  63.         LoggerInterface $logger
  64.     ) {
  65.         $this->environment $environment;
  66.         $this->cartFactory $cartFactory;
  67.         $this->cartPriceCalculatorFactory $cartPriceCalculatorFactory;
  68.         $this->orderManagers $orderManagers;
  69.         $this->logger $logger;
  70.     }
  71.     public function getCartClassName(): string
  72.     {
  73.         return $this->cartFactory->getCartClassName($this->environment);
  74.     }
  75.     /**
  76.      * checks if cart manager is initialized and if not, do so
  77.      */
  78.     protected function checkForInit()
  79.     {
  80.         if (!$this->initialized) {
  81.             $this->initSavedCarts();
  82.             $this->initialized true;
  83.         }
  84.     }
  85.     protected function initSavedCarts()
  86.     {
  87.         $carts $this->getAllCartsForCurrentUser();
  88.         if (empty($carts)) {
  89.             $this->carts = [];
  90.         } else {
  91.             foreach ($carts as $cart) {
  92.                 // check for order state of cart - remove it, when corresponding order is already committed
  93.                 $order $this->orderManagers->getOrderManager()->getOrderFromCart($cart);
  94.                 if (empty($order) || $order->getOrderState() !== $order::ORDER_STATE_COMMITTED) {
  95.                     $this->carts[$cart->getId()] = $cart;
  96.                 } else {
  97.                     // cart is already committed - cleanup cart and environment
  98.                     $this->logger->warning('Deleting cart with id {cartId} because linked order {orderId} is already committed.', [
  99.                         'cartId' => $cart->getId(),
  100.                         'orderId' => $order->getId(),
  101.                     ]);
  102.                     $cart->delete();
  103.                     $this->environment->removeCustomItem(CheckoutManager::CURRENT_STEP '_' $cart->getId());
  104.                     $this->environment->save();
  105.                 }
  106.             }
  107.         }
  108.     }
  109.     /**
  110.      * @return CartInterface[]|null
  111.      */
  112.     protected function getAllCartsForCurrentUser()
  113.     {
  114.         $classname $this->getCartClassName();
  115.         return $classname::getAllCartsForUser($this->environment->getCurrentUserId());
  116.     }
  117.     /**
  118.      * @param CheckoutableInterface $product
  119.      * @param int $count
  120.      * @param string|null $key
  121.      * @param string|null $itemKey
  122.      * @param bool $replace
  123.      * @param array $params
  124.      * @param array $subProducts
  125.      * @param string|null $comment
  126.      *
  127.      * @return null|string
  128.      *
  129.      * @throws InvalidConfigException
  130.      */
  131.     public function addToCart(
  132.         CheckoutableInterface $product,
  133.         $count,
  134.         $key null,
  135.         $itemKey null,
  136.         $replace false,
  137.         array $params = [],
  138.         array $subProducts = [],
  139.         $comment null
  140.     ) {
  141.         $this->checkForInit();
  142.         if (empty($key) || !array_key_exists($key$this->carts)) {
  143.             throw new InvalidConfigException(sprintf('Cart %s was not found.'$key));
  144.         }
  145.         $cart $this->carts[$key];
  146.         $itemKey $cart->addItem($product$count$itemKey$replace$params$subProducts$comment);
  147.         $this->save();
  148.         return $itemKey;
  149.     }
  150.     /**
  151.      * @return void
  152.      */
  153.     public function save()
  154.     {
  155.         $this->checkForInit();
  156.         foreach ($this->carts as $cart) {
  157.             $cart->save();
  158.         }
  159.     }
  160.     /**
  161.      * @param string|null $key
  162.      */
  163.     public function deleteCart($key null)
  164.     {
  165.         $this->checkForInit();
  166.         $this->getCart($key)->delete();
  167.         unset($this->carts[$key]);
  168.     }
  169.     /**
  170.      * @param array $params
  171.      *
  172.      * @return int|string
  173.      *
  174.      * @throws InvalidConfigException
  175.      */
  176.     public function createCart(array $params)
  177.     {
  178.         $this->checkForInit();
  179.         $cartId $params['id'] ?? null;
  180.         if ($cartId && isset($this->carts[$cartId])) {
  181.             throw new InvalidConfigException('Cart with id ' $params['id'] . ' already exists.');
  182.         }
  183.         if (!isset($params['name'])) {
  184.             throw new InvalidConfigException('Cart name is missing');
  185.         }
  186.         $cart $this->cartFactory->create($this->environment, (string)$params['name'], $cartId$params);
  187.         $cart->save();
  188.         $this->carts[$cart->getId()] = $cart;
  189.         return $cart->getId();
  190.     }
  191.     /**
  192.      * @param string|null $key
  193.      *
  194.      * @throws InvalidConfigException
  195.      */
  196.     public function clearCart($key null)
  197.     {
  198.         $this->checkForInit();
  199.         if (empty($key) || !array_key_exists($key$this->carts)) {
  200.             throw new InvalidConfigException(sprintf('Cart %s was not found.'$key));
  201.         }
  202.         $cart $this->carts[$key];
  203.         $newCart $this->cartFactory->create($this->environment$cart->getName(), $cart->getId());
  204.         $this->carts[$key] = $newCart;
  205.     }
  206.     /**
  207.      * @param string|null $key
  208.      *
  209.      * @return CartInterface
  210.      *
  211.      * @throws InvalidConfigException
  212.      */
  213.     public function getCart($key null)
  214.     {
  215.         $this->checkForInit();
  216.         if (empty($key) || !array_key_exists($key$this->carts)) {
  217.             throw new InvalidConfigException(sprintf('Cart %s was not found.'$key));
  218.         }
  219.         return $this->carts[$key];
  220.     }
  221.     /**
  222.      * @param string $name
  223.      *
  224.      * @return null|CartInterface
  225.      */
  226.     public function getCartByName($name)
  227.     {
  228.         $this->checkForInit();
  229.         foreach ($this->carts as $cart) {
  230.             if ($cart->getName() === $name) {
  231.                 return $cart;
  232.             }
  233.         }
  234.         return null;
  235.     }
  236.     /**
  237.      * @param string $name
  238.      *
  239.      * @return CartInterface
  240.      *
  241.      * @throws InvalidConfigException
  242.      */
  243.     public function getOrCreateCartByName($name)
  244.     {
  245.         $cart $this->getCartByName($name);
  246.         if (empty($cart)) {
  247.             $cartKey $this->createCart([
  248.                 'name' => $name,
  249.             ]);
  250.             $cart $this->getCart($cartKey);
  251.         }
  252.         return $cart;
  253.     }
  254.     /**
  255.      * @return CartInterface[]
  256.      */
  257.     public function getCarts(): array
  258.     {
  259.         $this->checkForInit();
  260.         return $this->carts;
  261.     }
  262.     /**
  263.      * @param string $itemKey
  264.      * @param string|null $key
  265.      *
  266.      * @throws InvalidConfigException
  267.      */
  268.     public function removeFromCart($itemKey$key null)
  269.     {
  270.         $this->checkForInit();
  271.         if (empty($key) || !array_key_exists($key$this->carts)) {
  272.             throw new InvalidConfigException(sprintf('Cart %s was not found.'$key));
  273.         }
  274.         $cart $this->carts[$key];
  275.         $cart->removeItem($itemKey);
  276.     }
  277.     /**
  278.      * @param CartInterface $cart
  279.      *
  280.      * @return CartPriceCalculatorInterface
  281.      */
  282.     public function getCartPriceCalculator(CartInterface $cart): CartPriceCalculatorInterface
  283.     {
  284.         return $this->cartPriceCalculatorFactory->create($this->environment$cart);
  285.     }
  286.     public function reset()
  287.     {
  288.         $this->carts = [];
  289.         $this->initialized false;
  290.     }
  291. }