简体   繁体   中英

display the number of items in the cart on virtuemart joomla 2.5?

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?

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;
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM