Find an existing item and update its quantity

@param LineItem $item     the item in the cart to update
@param int      $quantity the new quantity

@throws ValidationException
@return self
963594
public function updateItem($item, $quantity)
    {
        $stock_item = $item->FindStockItem();

        if (!$item instanceof LineItem) {
            throw new ValidationException(_t(
                "ShoppingCart.WrongItemClass",
                "Item needs to be of class {class}",
                ["class" => LineItem::class]
            ));
        }
        
        if ($item->Locked) {
            throw new ValidationException(_t(
                "ShoppingCart.UnableToEditItem",
                "Unable to change item's quantity"
            ));
        }

        // If we need to track stock, do it now
        if ($stock_item && ($stock_item->Stocked || $this->config()->check_stock_levels)) {
            if ($item->checkStockLevel($quantity) < 0) {
                $item->Quantity = $stock_item->StockLevel;
                $item->write();
                throw new ValidationException(_t(
                    "ShoppingCart.NotEnoughStock",
                    "There are not enough '{title}' in stock",
                    ['title' => $stock_item->Title]
                ));
            }
        }
        
        $item->Quantity = floor($quantity);
        $item->write();
        
        return $this;
    }
1100464
public function validate($item)
    {
        if (!$item) {
            $item = new \stdClass();
        }
        $validator = $this->getValidator();

        try {
            $validator->assert($item);
            return $this->populate($item);
        } catch (Exception $e) {
            throw new ConfigValidationFailedException(get_class($this), '', $e);
        }
    }
709685
public function addItem(Item $item) {
        if(empty($item->title) && empty($item->description)) {
            throw new Exception('At least one of title or description must be defined.');
        }

        $this->items[] = $item;
    }
335075
public function addItem(PricedItemInterface $item)
    {
        if ($this->hasItem($item)) {
            return $this;
        }

        foreach ($this->items as $existingItem) {
            if ($item->equals($existingItem)) {
                $existingItem->merge($item, false);

                $this->itemsTotal = null;
                $this->total = null;

                return $this;
            }
        }

        $item->setContainer($this);
        $this->items->add($item);

        $this->itemsTotal = null;
        $this->total = null;

        return $this;
    }
439676
public function add(Product $product, int $quantity)
    {
        if (!$product->isAvailable()) {
            throw new ProductNotAvailableException($product);
        }

        if (!$product->price()->hasCurrency($this->currency)) {
            throw new InvalidCurrencyException($this->currency, $product->price()->currency());
        }

        if (array_key_exists((string) $product->sku(), $this->items)) {
            $quantity += $this->items[(string) $product->sku()]->quantity();
        }

        $this->items[(string) $product->sku()] = new CartItem($product->sku(), $quantity);
    }
1257507
public function add($item, $attributes = [], $quantity = 1, $quantityReset = false)
    {
        if (!is_array($item) && !$item->isShoppable) return;

        $attributesHash = sha1(serialize($attributes));
        $cartItem = $this->getItem(is_array($item) ? $item['sku'] : $item->sku, $attributesHash);


        // Add new or sum quantity
        if (empty($cartItem)) {

            $arrAttributeObjects = [];
            if (!empty($attributes) && is_array($attributes)) {
                foreach ($attributes as $attributeType => $attributeValues) {
                    if ($type = Config::get('shop.attribute_models.' . $attributeType)) {
                        if (is_array($attributeValues)) {
                            foreach ($attributeValues as $cgr => $cAttr) {
                                $attr = new $type();
                                $attr->fillAttr($cgr, $cAttr);

                                $class = Config::get('shop.item_attributes');
                                $itemAttribute = new $class();

                                $itemAttribute->attributeObject()->associate($attr);

                                $arrAttributeObjects[] = $itemAttribute;

                            }
                        }
                    }
                }
            }

            $class = Config::get('shop.item');
            $cartItem = new $class();
            $cartItem->user_id = !empty($this->user->shopId) ? $this->user->shopId : 0;
            $cartItem->session_id = session('visitor_id');
            $cartItem->cart_id = $this->id;
            $cartItem->sku = is_array($item) ? $item['sku'] : $item->sku;
            $cartItem->price = is_array($item) ? $item['price'] : $item->getSpecificBasePrice($quantity, $arrAttributeObjects, $this);
            $cartItem->tax = is_array($item)
                                        ? (array_key_exists('tax', $item)
                                            ?   $item['tax']
                                            :   0
                                        )
                                        : (isset($item->tax) && !empty($item->tax)
                                            ?   $item->tax
                                            :   0
                                        );
            $cartItem->shipping = is_array($item)
                                        ? (array_key_exists('shipping', $item)
                                            ?   $item['shipping']
                                            :   0
                                        )
                                        : (isset($item->shipping) && !empty($item->shipping)
                                            ?   $item->shipping
                                            :   0
                                        );
            $cartItem->discount = $item->calculateDiscount();
            $cartItem->currency = Config::get('shop.currency');
            $cartItem->quantity = $quantity;
            $cartItem->attributes_hash = $attributesHash;

            // This looks a bit backwards but this is the proper way of storing the relation
            $item->shoppables()->save($cartItem);

            // Save the attributes
            $cartItem->itemAttributes()->saveMany($arrAttributeObjects);

            $this->resetCalculations();
        } else {
            $this->increase($cartItem, $quantity, $quantityReset);
        }

        return $this;
    }
1315649
public function setProductWarehouseStockAction(InventoryItem $item)
    {
        $em = $this->getDoctrine()->getManager();
        /* @var WarehouseProductService $warehouseProductService */
        $warehouseProductService = $this->get('flower.stock.service.warehouse_product');
        $warehouseProduct = $warehouseProductService->getByWarehouseProduct($item->getInventory()->getWarehouse(), $item->getProduct());
        $item->setStock($warehouseProduct->getStock());
        $em->flush();
        return $this->redirect($this->generateUrl('stock_inventory_show', array('id' => $item->getInventory()->getId())));
    }
1285741
public function checkStockLevel($qty)
    {
        $stock_param = $this->config()->stock_param;
        $item = $this->FindStockItem();
        $stock = ($item->$stock_param) ? $item->$stock_param : 0;
        
        // if not enough stock, throw an exception
        if($stock < $qty) {
            throw new ValidationException(_t(
                "Checkout.NotEnoughStock",
                "There are not enough '{title}' in stock",
                "Message to show that an item hasn't got enough stock",
                array('title' => $item->Title)
            ));
        }
    }
1276120
public function setItem(Item $item)
    {
        $this->childOf = $item->id;
        $this->_item = $item;
        
        return $this;
    }
188701
public function addItem(Item $item)
    {
        $itemUUID = $item->composeUUID();

        $this->elementsToUpdate[$itemUUID] = $item;
        unset($this->elementsToDelete[$itemUUID]);
    }
397426
public function removeItem(Item $item)
    {
        if ($this->items->contains($item)) {
            $this->items->removeElement($item);
            $item->removeLabel($this);
        }

        return $this;
    }
256469
public function addItem(Item $item)
    {
        if (!$this->items->contains($item)) {
            $this->items->add($item);
            $item->setStorage($this);
        }

        return $this;
    }
1080385
public function update($itemId, $quantity)
    {
        if ($cartItem = $this->get($itemId)) {
            $cartItem->canChangeQuantity($quantity);
            $cartItem->quantity = $quantity;
            $cartItem->save();
        }

        return $cartItem;
    }
370107
public function addItem($product, $quantity, $detail, $price)
    {
        $item = new stdClass();
        $item->product = $product;
        $item->quantity = $quantity;
        $item->detail = $detail;
        $item->price = $this->convertAmount($price);

        $this->data->items[] = $item;

        return $this;
    }
1160094
public function add(CartItem $cartItem)
    {
        $itemId = $cartItem->getId();

        // if item already exists in the cart, just update the quantity,
        // otherwise add it as a new item
        if ($this->has($itemId)) {
            $existingItem = $this->find($itemId);
            $existingItem->quantity += $cartItem->quantity;
        } else {
            $this->items[] = $cartItem;
        }
    }
1210983
public function add(Item $item){
    if(count($this->list) == 100){
      throw new OverflowException('This list is filled');
    }
    $this->list[] = $item;
  }
812173
public function update($item_key, $quantity)
    {
        $item = $this
            ->items
            ->find("Key", $item_key);
        
        if ($item && !$item->Locked) {
            // If we need to track stock, do it now
            if ($item->Stocked || $this->config()->check_stock_levels) {
                $item->checkStockLevel($quantity);
            }

            $item->Quantity = floor($quantity);
            
            $this->extend("onBeforeUpdate", $item);
            
            $this->save();
        } else {
            throw new Exception(_t("Checkout.UnableToEditItem", "Unable to change item's quantity"));
        }

        return false;
    }