WordPress.org

Ready to get started?Download WordPress

Forums

Simple Cart & Buy Now
[resolved] Simple Show Number Of Items In Cart? (28 posts)

  1. v8s_uk
    Member
    Posted 1 year ago #

    I've got 2 questions

    - Is there a simple way to show the number of items in the cart? For example to have php to echo the number of items currently in the cart so we can set this in the header 'view cart (3)'? similar to http://methodhome.com/category/home-cleaning/

    - Also is there a simple way to set free shipping?

    Thank you

    http://wordpress.org/extend/plugins/simple-cart-buy-now/

  2. bluey80
    Member
    Plugin Author

    Posted 1 year ago #

    I believe this will work for number of items:

    $cart = $_SESSION['wfcart'];
    echo count($cart->items)

    As for free shipping, shipping pricing for Paypal is set via your merchant settings on Paypal's site. For Google Wallet, take a look at scabn_getShippingOptions in templates/default.php -- you'll need to make a new template, but that file should describe what to do.

  3. v8s_uk
    Member
    Posted 1 year ago #

    Thanks for the help bluey, although the number of items that displays is for each individual product - so if someone adds 5 of one item it will still show as 1 item. Is there a way it will show the total amount of products in the basket?

    Excellent thanks for the heads up regarding free shipping, got that working fine.

  4. bluey80
    Member
    Plugin Author

    Posted 1 year ago #

    Oh, sorry, I misread. You'll need to loop over every item in the cart then. Something like

    $cart = $_SESSION['wfcart'];
    $count=0
    foreach($cart->items as $item){
        $count+= $cart->itemqtys[$item]
    }
  5. v8s_uk
    Member
    Posted 1 year ago #

    Thank you for the reply, although the code doesn't seem to work :( 'Parse error: syntax error, unexpected T_FOREACH in....'

  6. bluey80
    Member
    Plugin Author

    Posted 1 year ago #

    Hmm.. run

    $cart = $_SESSION['wfcart'];
    print_r($cart);

    and post the output (when you have items in your shopping cart).

  7. v8s_uk
    Member
    Posted 1 year ago #

    Thanks for the help. I've got 51 items in my cart and the output I get when running that is:

    wfCart Object
    (
        [items] => Array
            (
                [0] => test-product2-some-description
                [1] => test-product-bla-bla-bla
            )
    
        [itemprices] => Array
            (
                [test-product2-some-description] => 1
                [test-product-bla-bla-bla] => 1
            )
    
        [itemqtys] => Array
            (
                [test-product2-some-description] => 1
                [test-product-bla-bla-bla] => 50
            )
    
        [itemweight] => Array
            (
                [test-product2-some-description] => 400
                [test-product-bla-bla-bla] => 400
            )
    
        [itemname] => Array
            (
                [test-product2-some-description] => Test Product2
                [test-product-bla-bla-bla] => Test Product
            )
    
        [itemoptions] => Array
            (
                [test-product2-some-description] => Array
                    (
                        [test-product2] => some description
                    )
    
                [test-product-bla-bla-bla] => Array
                    (
                        [test-product] => Bla bla bla
                    )
    
            )
    
        [itemurl] => Array
            (
                [test-product2-some-description] => http://test.vision8studio.co.uk/?page_id=143
                [test-product-bla-bla-bla] => http://test.vision8studio.co.uk/?page_id=143
            )
    
        [total] => 51
    )
  8. bluey80
    Member
    Plugin Author

    Posted 1 year ago #

    Oh, You need a semicolon at the end of the $count line

    $cart = $_SESSION['wfcart'];
    $count=0
    foreach($cart->items as $item){
        $count+= $cart->itemqtys[$item];
    }
  9. v8s_uk
    Member
    Posted 1 year ago #

    I had tried that but still no luck :(

  10. bluey80
    Member
    Plugin Author

    Posted 1 year ago #

    Same error? Also, I missed a semicolon on $count:

    $cart = $_SESSION['wfcart'];
    $count=0;
    foreach($cart->items as $item){
        $count+= $cart->itemqtys[$item];
    }
  11. v8s_uk
    Member
    Posted 1 year ago #

    Yep the error I got before, which is:

    Parse error: syntax error, unexpected T_FOREACH in..

    Although after putting in the second semi colon it comes up with nothing (blank).

    On a separate note, from doing this I've noticed when you refresh the checkout page the first item in the cart incrementes by 1 each time? this might just be mine, I haven't trouble shooted this but thought I'd just mention it.

  12. v8s_uk
    Member
    Posted 1 year ago #

    Woops, actually ignore that last bit about the quantity incrementing!

  13. bluey80
    Member
    Plugin Author

    Posted 1 year ago #

    Right, this is just the code to get the number. If you want to print the number of (total) items. Just add at the end that code

    echo $count;

    My guess about the incrementing count is that you are using that add to cart option to go directly to the checkout page. When you do this, the link you click says "add one item and go to checkout page". When you hit reload from the checkout page, it reloads this command. You can test this but going to another page and click on the "go to checkout" button from the sidebar and then see if reload still increments the count.

  14. v8s_uk
    Member
    Posted 1 year ago #

    I've tried this:
    <?php
    $cart = $_SESSION['wfcart'];
    $count=0;
    foreach($cart->items as $item){
    $count+= $cart->itemqtys[$item];
    echo $count;
    }
    ?>
    But it causes a strange output, which is, say I have added 2 of item 1 and 2 of item 2 it will display like this '24' it adds up the total items and displays them next to each other? If I added 1 of item 1, 2 of item 2 and 3 of item 3 it was display 136 adding 1,(1+2)3,(3+3)6

    Yes you're spot on, the reason it was incrementing was because I was reloading the command page.

  15. bluey80
    Member
    Plugin Author

    Posted 1 year ago #

    Oh, it is treating count a string, not an integer. Try:

    $cart = $_SESSION['wfcart'];
    $count=0;
    foreach($cart->items as $item){
        $count+= intval($cart->itemqtys[$item]);
    }
    echo $count;

    Also make sure the echo $count; line is after you close the foreach loop.

  16. v8s_uk
    Member
    Posted 1 year ago #

    Ah yes that works perfectly, thank you! :)

  17. thisisedie
    Member
    Posted 1 year ago #

    When I put this code where I want the total items to appear, I get this error: Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\wordpress\wp-content\plugins\exec-php\includes\runtime.php(42) : eval()’d code on line 13 0.

  18. bluey80
    Member
    Plugin Author

    Posted 1 year ago #

    You probably have a typo in the code -- can you paste your exact php code.

  19. thisisedie
    Member
    Posted 1 year ago #

    Im putting this on the store page so it says something like "you have this many items in your shopping cart".

    <?php
    $cart = $_SESSION['wfcart'];
    $count=0;
    foreach($cart->items as $item){
        $count+= intval($cart->itemqtys[$item]);
    }
    echo $count;
    ?>
  20. bluey80
    Member
    Plugin Author

    Posted 1 year ago #

    Where are you placing this code? Try

    $cart = $_SESSION['wfcart'];
    print_r($cart);
    and send the output. It looks like it isn't getting the cart data would be my guess.

  21. thisisedie
    Member
    Posted 1 year ago #

    I'm putting it at the top of a WordPress page.

    $cart = $_SESSION['wfcart'];
    print_r($cart);

    comes up with no output?

    Thanks so much for your help :)

  22. bluey80
    Member
    Plugin Author

    Posted 1 year ago #

    This is php -- it shouldn't go in a wordpress page but in the plugin code. Take a look at SCABN's templates/default.php for some examples and making your own template that has custom php code.

  23. thisisedie
    Member
    Posted 1 year ago #

    Where do I find SCABN's templates?

  24. thisisedie
    Member
    Posted 1 year ago #

    And would there be a shortcode to call the total number of items on a WordPress page?

  25. bluey80
    Member
    Plugin Author

    Posted 1 year ago #

    In your filesystem, under WordPress install directory will be
    wp-content/plugins/simple-cart-buy-now/templates/default.php

    There isn't a shortcode for the total number of items, although you could add one. What v8s_uk did was put that number in the shopping cart output.

  26. thisisedie
    Member
    Posted 1 year ago #

    Im using WordPress Simple Paypal Shopping Cart, not the Simple Cart Buy Now. I'm sorry I'm not super good at this stuff. How could I add a shortcode for the total items for WordPress Simple Paypal Shopping Cart?

  27. bluey80
    Member
    Plugin Author

    Posted 1 year ago #

    You should ask in the forum for WordPress Simple Paypal Shopping Cart

  28. thisisedie
    Member
    Posted 1 year ago #

    Oh my. I'm so sorry. I thought this was for the same plugin. I do have a post there. This is what I get for not sleeping for a few days. Sigh.

Topic Closed

This topic has been closed to new replies.

About this Plugin

About this Topic