public function addPurchasable(
        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 (
                (get_class($cartLine->getPurchasable()) === get_class($purchasable)) &&
                ($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;
    }
public function addPurchasable(
        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 (
                (get_class($cartLine->getPurchasable()) === get_class($purchasable)) &&
                ($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;
    }
public function addPurchasable ( 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 ( ( get_class ( $ cartLine -> getPurchasable ( ) ) === get_class ( $ purchasable ) ) && ( $ 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 ; }
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 int                  $quantity    Number of units to set or increase

@return $this Self object
Add a Purchasable to Cart as a new CartLine .