예제 #1
0
 /**
  * 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);
 }
 /**
  * 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);
 }
 /**
  * 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;
 }
예제 #4
0
 /**
  * Adds cartLine to Cart.
  *
  * This method dispatches all Cart Check and Load events
  * It should NOT be used to add a Purchasable to a Cart,
  * by manually passing a newly crafted CartLine, since
  * no product duplication check is performed: in that
  * case CartManager::addProduct should be used
  *
  * @param CartInterface     $cart     Cart
  * @param CartLineInterface $cartLine Cart line
  *
  * @return $this Self object
  */
 private function addLine(CartInterface $cart, CartLineInterface $cartLine)
 {
     $cartLine->setCart($cart);
     $cart->addCartLine($cartLine);
     $this->cartLineEventDispatcher->dispatchCartLineOnAddEvent($cart, $cartLine);
     $this->cartEventDispatcher->dispatchCartLoadEvents($cart);
     return $this;
 }
예제 #5
0
 /**
  * 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;
 }
예제 #6
0
 /**
  * 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;
 }
예제 #8
0
 /**
  * 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 integer           $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;
 }
예제 #9
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 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;
 }