Reputation: 594
I'm using the code below to display a number based on the number of items in the cart. If there's 1 item, then the number 1 is generated, 2 items and 2 is displayed etc etc.
the problem is that it displays the quantity for the product with an id of [1]. how can change this to make it work for all product ids?
<?php $array = unserialize($_SESSION['__vm']['vmcart']);
$amount = $array->products[1]->amount;
if ($amount != 0){ echo $amount; } else { echo 0; } ?>
the [1] is the product id. how do i change it to accept all product ids?
Upvotes: 0
Views: 2273
Reputation: 3273
Are you wanting to loop through all products? Something like ...
<?php
$array = unserialize($_SESSION['__vm']['vmcart']);
foreach($array->products as $product){
$amount = $product->amount;
if ($amount != 0){ echo $amount; } else { echo 0; }
}
?>
Adding all products ...
<?php
$array = unserialize($_SESSION['__vm']['vmcart']);
$total = 0;
foreach($array->products as $product){
$total += $product->amount;
}
echo "Total Products: " . $total;
?>
Upvotes: 1