SilverCart Forum

We moderate this Forum and we're here to help. Have you already run a forum search to check if your problem has already been solved?

You can help us helping you by providing detailed error messages, screenshots and logfile entries.

SebastianRamon

Page: 1
Topic Total Quantity 2462 Views

Total Quantity

5 October 2012 at 6:22am

I'm using SilverCart for the first time - I love it.

I couldn't find a function that counted the number of items in a shopping cart, so I quickly put this together:

Put in Page.php for global access

function getQuantityInCart(){
$qty = 0;
if($member = Member::currentUser()){
if($cart = $member->Cart){

if(count($cart->SilvercartShoppingCartPositions())>0)
foreach($cart->SilvercartShoppingCartPositions() as $product){
$qty += $product->Quantity;
}
}
}
return $qty;
}

Call it using

Number of items in cart: $Top.QuantityInCart

I'm sure there's a cleaner way to do it (decorate the shoppingcart maybe?) but for what I needed, it worked.

Hope it helps someone!

Re: Total Quantity

5 October 2012 at 1:33pm Last edited: 5 October 2012 1:49pm

Hi txc88,

nice to hear that you like SilverCart as much as we do.

There is already a method called "getQuantity()" located in SilvercartShoppingCart.php which should work for you. We're using it to display the quantity in the headerbar in SilverCart´s default template.

function getQuantityInCart(){
$qty = 0;

if($member = Member::currentUser()){
if($cart = $member->Cart){
$qty = $cart->getQuantity();
}
}
}

return $qty;
}

should do the job.

Cheers,
Patrick

Re: Total Quantity

16 October 2012 at 6:35am

Thanks for that Patrick!

That looks much neater.