Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Help with shopping cart total code

    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.

    Code:
    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.
    Attached Thumbnails Attached Thumbnails Help with shopping cart total code-basket-screenshot.gif  

  • #2
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Try this:
    PHP Code:
    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)
    Last edited by Wanna; 09-19-2011 at 10:38 AM.

  • #3
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    edit the function change this bit

    PHP Code:
    return $grand_total
    to this

    PHP Code:
    return number_format($grand_total,2); 
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #4
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Quote Originally Posted by hinch View Post
    edit the function change this bit

    PHP Code:
    return $grand_total
    to this

    PHP Code:
    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)

  • #5
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Wanna View Post
    Try this:
    PHP Code:
    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

  • #6
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    Quote Originally Posted by Wanna View Post
    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.
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #7
    Regular Coder
    Join Date
    Sep 2011
    Posts
    128
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Quote Originally Posted by hinch View Post
    the advantage of number_format over sprintf is that number_format correctly follows rounding rules sprintf does not.
    This i didn't know.
    Thanks.

  • #8
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by hinch View Post
    edit the function change this bit

    PHP Code:
    return $grand_total
    to this

    PHP Code:
    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

  • #9
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Wanna View Post
    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


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •