/**
  * Check CartLine integrity.
  *
  * When a purchasable is not enabled or its quantity is <=0,
  * the line is discarded and a ElcodiCartEvents::CART_INCONSISTENT
  * event is fired.
  *
  * A further check on stock availability is performed so that when
  * $quantity is greater that the available units, $quantity for this
  * CartLine is set to Purchasable::$stock number
  *
  * @param CartLineInterface $cartLine Cart line
  *
  * @return CartLineInterface CartLine
  */
 private function validateCartLine(CartLineInterface $cartLine)
 {
     $cart = $cartLine->getCart();
     $purchasable = $cartLine->getPurchasable();
     $realStockAvailable = $this->purchasableStockValidator->isStockAvailable($purchasable, $cartLine->getQuantity(), $this->useStock);
     if (false === $realStockAvailable || $realStockAvailable === 0) {
         $this->cartManager->silentRemoveLine($cart, $cartLine);
         /**
          * An inconsistent cart event is dispatched.
          */
         $this->cartEventDispatcher->dispatchCartInconsistentEvent($cart, $cartLine);
         return $cartLine;
     }
     if (is_int($realStockAvailable)) {
         $cartLine->setQuantity($realStockAvailable);
     }
     return $cartLine;
 }
Esempio n. 2
0
 /**
  * Check CartLine integrity
  *
  * When a purchasable is not enabled or its quantity is <=0,
  * the line is discarded and a ElcodiCartEvents::CART_INCONSISTENT
  * event is fired.
  *
  * A further check on stock availability is performed so that when
  * $quantity is greater that the available units, $quantity for this
  * CartLine is set to Purchasable::$stock number
  *
  * @param CartLineInterface $cartLine Cart line
  *
  * @return CartLineInterface CartLine
  */
 protected function checkCartLine(CartLineInterface $cartLine)
 {
     $cart = $cartLine->getCart();
     $purchasable = $cartLine->getPurchasable();
     if (!$purchasable instanceof PurchasableInterface || !$purchasable->isEnabled() || $cartLine->getQuantity() <= 0) {
         $this->cartManager->silentRemoveLine($cart, $cartLine);
         /**
          * An inconsistent cart event is dispatched
          */
         $this->cartEventDispatcher->dispatchCartInconsistentEvent($cart, $cartLine);
     }
     /**
      * We cannot exceed available stock for a given purchasable
      * when setting CartLine::$quantity
      */
     if ($cartLine->getQuantity() > $purchasable->getStock()) {
         $cartLine->setQuantity($purchasable->getStock());
     }
     return $cartLine;
 }
Esempio n. 3
0
 /**
  * Check CartLine integrity
  *
  * When a purchasable is not enabled or its quantity is <=0,
  * the line is discarded and a ElcodiCartEvents::CART_INCONSISTENT
  * event is fired.
  *
  * A further check on stock availability is performed so that when
  * $quantity is greater that the available units, $quantity for this
  * CartLine is set to Purchasable::$stock number
  *
  * @param CartLineInterface $cartLine Cart line
  *
  * @return CartLineInterface CartLine
  */
 private function checkCartLine(CartLineInterface $cartLine)
 {
     $cart = $cartLine->getCart();
     $purchasable = $cartLine->getPurchasable();
     if (!$purchasable instanceof PurchasableInterface || !$purchasable->isEnabled() || $this->useStock && $cartLine->getQuantity() <= 0) {
         $this->cartManager->silentRemoveLine($cart, $cartLine);
         /**
          * An inconsistent cart event is dispatched
          */
         $this->cartEventDispatcher->dispatchCartInconsistentEvent($cart, $cartLine);
     }
     /**
      * We cannot exceed available stock for a given purchasable
      * when setting CartLine::$quantity
      *
      * This checking has sense when the Product has not infinite stock
      */
     if ($this->useStock && $cartLine->getProduct()->getStock() !== ElcodiProductStock::INFINITE_STOCK && $cartLine->getQuantity() > $purchasable->getStock()) {
         $cartLine->setQuantity($purchasable->getStock());
     }
     return $cartLine;
 }