/** * Test decrease cartline quantity. * * @param mixed $quantityStart Starting quantity * @param mixed $quantityRemoved Quantity to remove * @param mixed $quantityEnd Quantity to check against * * @dataProvider dataDecreaseCartLineQuantity */ public function testDecreaseCartLineQuantity($quantityStart, $quantityRemoved, $quantityEnd) { $this->cartLine->setQuantity($quantityStart); $this->get('elcodi.manager.cart')->addPurchasable($this->cart, $this->cartLine->getPurchasable(), $this->cartLine->getQuantity()); $line = $this->cart->getCartLines()->last(); $this->get('elcodi.manager.cart')->decreaseCartLineQuantity($line, $quantityRemoved); $this->assertResults($quantityEnd); }
/** * Test edit cart line * * @group cart */ public function testEditCartLine() { $this->get('elcodi.manager.cart')->addPurchasable($this->cart, $this->cartLine->getProduct(), $this->cartLine->getQuantity()); $line = $this->cart->getCartLines()->last(); $this->get('elcodi.manager.cart')->editCartLine($line, $this->purchasable, 2); $this->assertSame($this->cartLine->getPurchasable(), $this->purchasable); $this->assertEquals($this->cart->getAmount(), $this->cart->getCartLines()->first()->getAmount()); $this->assertTrue($this->cart->getAmount()->equals($this->cart->getCartLines()->first()->getPurchasable()->getPrice()->multiply(2))); }
/** * Performs all processes to be performed after the order creation. * * Flushes all loaded order and related entities. * * @param CartLineInterface $cartLine Cart line */ public function updatePurchasableStockByCartLine(CartLineInterface $cartLine) { $purchasable = $cartLine->getPurchasable(); $stock = $purchasable->getStock(); if ($stock === ElcodiProductStock::INFINITE_STOCK) { return; } $quantityPurchased = $cartLine->getQuantity(); $newStock = $stock - $quantityPurchased; $newStock = max($newStock, 0); $purchasable->setStock($newStock); $this->flushPurchasable($purchasable); }
/** * Given a cart line, creates a new order line * * @param OrderInterface $order Order * @param CartLineInterface $cartLine Cart Line * * @return OrderLineInterface OrderLine created */ public function createOrderLineByCartLine(OrderInterface $order, CartLineInterface $cartLine) { $orderLine = $cartLine->getOrderLine() instanceof OrderLineInterface ? $cartLine->getOrderLine() : $this->orderLineFactory->create(); /** * @var OrderLineInterface $orderLine */ $orderLine->setOrder($order)->setPurchasable($cartLine->getPurchasable())->setQuantity($cartLine->getQuantity())->setProductAmount($cartLine->getProductAmount())->setAmount($cartLine->getAmount())->setHeight($cartLine->getHeight())->setWidth($cartLine->getWidth())->setDepth($cartLine->getDepth())->setWeight($cartLine->getWeight()); $this->orderLineEventDispatcher->dispatchOrderLineOnCreatedEvent($order, $cartLine, $orderLine); return $orderLine; }
/** * Loads CartLine prices. * This method does not consider Coupon. * * @param CartLineInterface $cartLine Cart line * * @return CartLineInterface Line with prices loaded */ private function loadCartLinePrices(CartLineInterface $cartLine) { $purchasable = $cartLine->getPurchasable(); $purchasablePrice = $purchasable->getPrice(); /** * If present, reducedPrice will be used as purchasable price in current CartLine. */ if ($purchasable->getReducedPrice()->getAmount() > 0) { $purchasablePrice = $purchasable->getReducedPrice(); } /** * Setting amounts for current CartLine. * * Line Currency was set by CartManager::addPurchasable when factorizing CartLine */ $cartLine->setPurchasableAmount($purchasablePrice); $cartLine->setAmount($purchasablePrice->multiply($cartLine->getQuantity())); return $cartLine; }
/** * Performs all processes to be performed after the order creation. * * Flushes all loaded order and related entities. * * @param CartLineInterface $cartLine Cart line */ public function updatePurchasableStockByCartLine(CartLineInterface $cartLine) { $purchasable = $cartLine->getPurchasable(); $this->purchasableStockUpdater->updateStock($purchasable, $cartLine->getQuantity()); }
/** * 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(); 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; }