Exemplo n.º 1
0
 /**
  * Add a Purchasable to Cart as a new CartLine
  *
  * This method creates a new CartLine and set item quantity
  * correspondingly.
  *
  * If the Purchasable is already in the Cart, it just increments
  * item quantity by $quantity
  *
  * @param CartInterface        $cart        Cart
  * @param PurchasableInterface $purchasable Product or Variant to add
  * @param integer              $quantity    Number of units to set or increase
  *
  * @return $this self Object
  */
 public function addProduct(CartInterface $cart, PurchasableInterface $purchasable, $quantity)
 {
     /**
      * If quantity is not a number or is 0 or less, product is not added
      * into cart
      */
     if (!is_int($quantity) || $quantity <= 0) {
         return $this;
     }
     foreach ($cart->getCartLines() as $cartLine) {
         /**
          * @var CartLineInterface $cartLine
          */
         if ($cartLine->getPurchasable()->getId() == $purchasable->getId()) {
             /**
              * Product already in the Cart, increase quantity
              */
             return $this->increaseCartLineQuantity($cartLine, $quantity);
         }
     }
     $cartLine = $this->cartLineFactory->create();
     $cartLine->setPurchasable($purchasable)->setQuantity($quantity);
     $this->addLine($cart, $cartLine);
     return $this;
 }