3

I just want to know the syntax of adding the quantity using foreach. Any help is greatly appreciable.

Here is the code what I do so far:

$qproducts = '0';
foreach ($this->cart->getProducts() as $product) {  
    $qproducts .= $product['quantity'];
}       
$this->data['pquantity'] = $qproducts;

2 Answers 2

5

You mean like this?

$qproducts = 0;
foreach ($this->cart->getProducts() as $product) {  
    $qproducts += $product['quantity'];
}       
$this->data['pquantity'] = $qproducts;
Sign up to request clarification or add additional context in comments.

Comments

4

Use + (addition) instead of . (concatenation).

$qproducts = 0;
foreach ($this->cart->getProducts() as $product) {  
    $qproducts += $product['quantity'];
}   

1 Comment

Thanx alot Mark and Kemal. I dont know how it just skipped out of my mind.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.