/** * Sets quantity to cartLine. * * If quantity is higher than item stock, throw exception * * This method dispatches all Cart Check and Load events * * @param CartLineInterface $cartLine Cart line * @param int $quantity CartLine quantity to set * * @return $this Self object */ public function setCartLineQuantity(CartLineInterface $cartLine, $quantity) { $cart = $cartLine->getCart(); if (!$cart instanceof CartInterface) { return $this; } /** * If $quantity is an integer and is less or equal than 0, means that * full line must be removed. * * Otherwise, $quantity can have two values: * * null or false - Quantity is not affected * * integer higher than 0, quantity is edited and all changes are * recalculated. */ if (is_int($quantity) && $quantity <= 0) { $this->silentRemoveLine($cart, $cartLine); } elseif (is_int($quantity)) { $cartLine->setQuantity($quantity); $this->cartLineEventDispatcher->dispatchCartLineOnEditEvent($cart, $cartLine); } else { /** * Nothing to do here. Quantity value is not an integer, so will not * be treated as such. */ return $this; } $this->cartEventDispatcher->dispatchCartLoadEvents($cart); return $this; }
/** * 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; }
/** * 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; }
/** * 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; }