...

View Full Version : Help with shopping cart total code



Margaret Mairs
09-19-2011, 09:05 AM
Hi,

I really hope someone can help me please.

I am using the Simple PayPal Shopping Cart in my website. I am building this on my local machine at the moment using the WAMP server.

I wanted to show a count in the header so that users will know when a product is added to the basket and found the following script after much searching.



If you want to display the shopping cart total in the sidebar or somewhere similar then add the following function to the "wp_shopping_cart.php":

function simple_cart_total()
{
foreach ($_SESSION['simpleCart'] as $item)
{
$total += $item['price'] * $item['quantity'];
$item_total_shipping += $item['shipping'] * $item['quantity'];
}
$grand_total = $total + $item_total_shipping;
return $grand_total;
}

Now you can simply call the "simple_cart_total" function and echo it out from your sidebar.php file:

echo simple_cart_total();



It's the yellow bit I need to tweak somehow. Although this works fine it shows the total in basket as 0 and I would like it to show as 0.00 (for instance it shows 1.50 as 1.5 which doesn't look right).

I hope someone can help me please.

Thank you.

Wanna
09-19-2011, 10:33 AM
Try this:


echo sprintf("%01.2f", simple_cart_total());


In this case, the sprintf will always make sure there are 2 decimals.
So if the function would return 1,1 the sprintf makes 1.10 from it.

(From 0 it makes 0.00)

hinch
09-19-2011, 10:46 AM
edit the function change this bit


return $grand_total;

to this


return number_format($grand_total,2);

Wanna
09-19-2011, 10:54 AM
edit the function change this bit


return $grand_total;

to this


return number_format($grand_total,2);

This could also work.
But if you put it in the function you can't use the short version anywhere else.
Sometimes this is needed (Atleast when i make shopping cart)

Margaret Mairs
09-19-2011, 10:57 AM
Try this:


echo sprintf("%01.2f", simple_cart_total());


In this case, the sprintf will always make sure there are 2 decimals.
So if the function would return 1,1 the sprintf makes 1.10 from it.

(From 0 it makes 0.00)

Hi Wanna,

This worked a treat, thank you so much.

Kind regards
Margaret

hinch
09-19-2011, 10:58 AM
This could also work.
But if you put it in the function you can't use the short version anywhere else.
Sometimes this is needed (Atleast when i make shopping cart)

the better method would be for you to simply style your shortened version to be smaller and allow for the .00 on the end as strictly currency values should always have the decimal values there.

failing that ofc you could always apply another sub num format to remove the decimals on your echo of your shortened version.

the advantage of number_format over sprintf is that number_format correctly follows rounding rules sprintf does not.

Wanna
09-19-2011, 11:00 AM
the advantage of number_format over sprintf is that number_format correctly follows rounding rules sprintf does not.

This i didn't know.
Thanks.

Margaret Mairs
09-19-2011, 11:01 AM
edit the function change this bit


return $grand_total;

to this


return number_format($grand_total,2);

Hi Hinch,

I now have it working thanks to Wanna however thank you for your help.

Can I just say that I have posted this problem on 3 other forums and no one even bothered to reply to me.

Thanks you guys.

Kind Regards
Margaret

Margaret Mairs
09-19-2011, 11:09 AM
This could also work.
But if you put it in the function you can't use the short version anywhere else.
Sometimes this is needed (Atleast when i make shopping cart)

Hi Hitch,

After looking at your discussion with Wanna I have now changed to your method and this also works a treat. As I am only wanting to display the total in the header of the site - there is no need to display it anywhere else.

Thank you both.

Kind regards
Margaret



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum