when i pass third parameter $qty in my add function
Where is your third parameter? public function add($item, $id)
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
Hi, Below was the code i add product to cart it works fine when in add product to card with no specific quantity (only one product at once),
Code
public function add($item, $id){
$storedItem = ['qty' => 0, 'price' => $item->price, 'item' => $item];
if($this->items) {
if(array_key_exists($id, $this->items)) {
$storedItem = $this->items[$id];
}
}
$storedItem['qty']++;
$storedItem['price'] = $item->price * $storedItem['qty'];
$this->items[$id] = $storedItem;
$this->totalQty++;
$this->totalPrice += $item->price;
}
But when i pass third parameter $qty in my add function (e.g i add 3) it become add the 4 product i know it increment one as it was fine. but the total quantity is not incremented why?
Please advice
public function add($item, $id, $qty=1)
{
$storedItem = ['qty' => 0, 'price' => $item->price, 'item' => $item];
if($this->items) {
if(array_key_exists($id, $this->items)) {
$storedItem = $this->items[$id];
}
}
$storedItem['qty'] += $qty;
$storedItem['price'] = $item->price * $storedItem['qty'];
$this->items[$id] = $storedItem;
$this->totalQty += $qty;
$this->totalPrice += ($item->price * $qty);
}
Please or to participate in this conversation.