Support » Plugin: Simple Cart & Buy Now » Simple Show Number Of Items In Cart?

Viewing 15 replies - 1 through 15 (of 27 total)
  • Plugin Author bluey80

    (@bluey80)

    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.

    Thread Starter v8s_uk

    (@v8s_uk)

    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.

    Plugin Author bluey80

    (@bluey80)

    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]
    }

    Thread Starter v8s_uk

    (@v8s_uk)

    Thank you for the reply, although the code doesn’t seem to work 🙁 ‘Parse error: syntax error, unexpected T_FOREACH in….’

    Plugin Author bluey80

    (@bluey80)

    Hmm.. run

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

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

    Thread Starter v8s_uk

    (@v8s_uk)

    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
    )
    Plugin Author bluey80

    (@bluey80)

    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];
    }

    Thread Starter v8s_uk

    (@v8s_uk)

    I had tried that but still no luck 🙁

    Plugin Author bluey80

    (@bluey80)

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

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

    Thread Starter v8s_uk

    (@v8s_uk)

    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.

    Thread Starter v8s_uk

    (@v8s_uk)

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

    Plugin Author bluey80

    (@bluey80)

    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.

    Thread Starter v8s_uk

    (@v8s_uk)

    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.

    Plugin Author bluey80

    (@bluey80)

    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.

    Thread Starter v8s_uk

    (@v8s_uk)

    Ah yes that works perfectly, thank you! 🙂

Viewing 15 replies - 1 through 15 (of 27 total)
  • The topic ‘Simple Show Number Of Items In Cart?’ is closed to new replies.