Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

tehseen's avatar

Add to cart qty not working,

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

0 likes
16 replies
ouhare's avatar

when i pass third parameter $qty in my add function

Where is your third parameter? public function add($item, $id)

tehseen's avatar

@ouhare i need to ask this is default function, and its working fine but when pass dynamic quantity then this happen.

This is my controller function when i add a quantity: i have pass the dynamic value when it works instead of 2(for now i just add to explain you when i add more then once.)

$cart->add($product, $product->id, 2);

My cart model function where i add product to cart

public function add($item, $id, $qty){
        $storedItem = ['qty' => $qty, '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;
    }

then this happen

Cart {#308 ▼
  +items: array:1 [▼
    1 => array:3 [▼
      "qty" => 3
      "price" => 136
      "item" => Product {#297 ▶}
    ]
  ]
  +totalQty: 2
  +totalPrice: 68
}

which is wrong

tehseen's avatar

the calculation also goes wrong total price must be 102

Snapey's avatar

I would start again. and think it through properly.

Why must price be 102?

oh, and don't mention specific people in your question because everyone else will not bother to answer

tehseen's avatar

Yes sure @Snapey . Please help in the above code. I want to fixed the every others product with 1 quantity at a time but when i add the product via search page i need dynamic. So in this 2nd case it mess with calculation and quantity issue.

Snapey's avatar

because the code is rubbish. Honestly, it would be better to start again.

tehseen's avatar

would you please help me to do this. i need a quantity if some one not chose it will default 1 and if some one select it will be that selected quantity. But if need same array of cart as i already do a lot of code.

ouhare's avatar

@tehseen What happen if your $item already exists in your $this->items array ? You can set $qtywith any number you want, it will pick up the quantity of $this->items[$id].... I guess

tehseen's avatar

when a item already exist in my session(cart). with default setting with out qty parameter it add the product (if its same as already exits it increment if not add another product in cart) this works fine.

But if i add the dynamic quantity it mess up with orders quantity and total quantity and price.

Snapey's avatar
Snapey
Best Answer
Level 122
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);
}
tehseen's avatar

So can i pass $qty to what ever quantity i need ? of if not it will be only add one item ?

tehseen's avatar

@Snapey i have another query related to amazon fulfillment service i need to sent my store orders to amazon for shipping service please advice.

rubenochoa's avatar

If you want to add image at this case what you can do?

Please or to participate in this conversation.