News:

Looking for documentation? Take a look on our wiki

Main Menu

How to display number of products in cart?

Started by samlf3rd, March 26, 2013, 14:20:33 PM

Previous topic - Next topic

samlf3rd

*****UPDATE******
I have created a very cool module that will show the cart number, and it is easily customizable. You can get it here: http://www.samswebextensions.com

I am running Virtuemart 2.0.18. What I need to accomplish is to just display the total number of products in the cart with a number like "10". I have a cart image and want to place that next to the image.

I have found similar questions-but, the answers did not apply-nor work for my situation.

I did find this thread on stackoverflow: display the number of items in the cart on virtuemart joomal 2.5?

That solution did not solve my problem:

<?php 
    $array 
unserialize($_SESSION['__vm']['vmcart']); 
    
$total 0;
    foreach(
$array->products as $product){
        
$total += $product->amount;
    }
    echo 
"Total Products: " $total;
?>


This only works if I go to my cart (as the cart is ran by cookies). The php isn't working also if I add a few products the number count is off. Basically the whole code does not function at all.

Is there a JavaScript that will work to access the cookies and retrieve the amount of products in the cart, or is there a similar way to do this with PHP?

Thank you in advance for your help, I really appreciate it!

*****UPDATE******
I have created a very cool module that will show the cart number, and it is easily customizable. You can get it here: http://www.samswebextensions.com

K&K media production

please look in the code of virtuemart cart modul, there you will find what you need

also you can use this modul for your needs, make a override and show only your image and the number of total products

samlf3rd

Quote from: kkmediaproduction on March 26, 2013, 21:06:26 PM
please look in the code of virtuemart cart modul, there you will find what you need

also you can use this modul for your needs, make a override and show only your image and the number of total products
I looked in the mini-cart mod and I see the code that displays the number of products, but when I place it in my template I get white screens on my site:
<div class="total" style="float: right;">
<?php if ($data->totalProduct and $show_price) echo  $data->billTotal?>
</div>
<div class="total_products"><?php echo  $data->totalProductTxt ?></div>
<div class="show_cart">
<?php if ($data->totalProduct and $show_price) echo  $data->cart_show?>
</div>
<div style="clear:both;"></div>


Am I missing something here? Thanks for the reply,
Sam

PRO


samlf3rd

Quote from: PRO on March 26, 2013, 22:56:51 PM
samlf3rd,
dont put it in the templae.

JUST change the cart module, and use it

http://forum.virtuemart.net/index.php?topic=113943.0

Pro, that is what I was thinking, but I already have the cart module on the page built into the template.
Can I duplicate the module somehow and remove the code and use it like that?

samlf3rd

Okay I figured it out, and now I am working on a nice CSS solution for my templates.

I will post the file here if anyone wants to use it-seems super useful, and nearly every site I come across has a something that looks like this: Cart3 <--Meaning that 3 products are in your cart instead of just linking the cart text or a shopping cart image. Cool! Thanks for your help I will post mod soon.

samlf3rd

#6
Here is the file if anyone would like to try it out. I am using Joomla 2.5 with Virtuemart 2.2
I removed the names and classes so you don't get CSS results from your normal Virtuemart Shopping Cart. This module only displays the number of products in a cart, and has some CSS applied directly to the default.php inside of the tmpl folder.

Thank you for your help-enjoy!

*Update-I posted a better version a couple posts down from here, so I removed the first package.

PRO


samlf3rd

#8
Quote from: PRO on March 27, 2013, 12:22:44 PM
what does it look like?

post a picture?

Here you go, I even added a couple things to the new pack.
I did find that since this module works off of cookies for some reason my slideshow on my home page isn't working with my jQuery. I guess I will have to find a new slideshow-as mine uses the address bar to display the slide-number in it.

I have the Cart text linked to the cart like this: /index.php/view-cart change it if you need to or the CSS in /modules/mod_virtuemart_cart2/tmpl/default.php.



[attachment cleanup by admin]

samlf3rd

One problem is that it doesn't show "0" if there are no products in there, I would like to make that happen with an if/else statement.

PRO

Quote from: samlf3rd on March 27, 2013, 15:44:49 PM
One problem is that it doesn't show "0" if there are no products in there, I would like to make that happen with an if/else statement.

what does it say?

Mine says empty cart

samlf3rd

Quote from: PRO on March 27, 2013, 20:03:19 PM
Quote from: samlf3rd on March 27, 2013, 15:44:49 PM
One problem is that it doesn't show "0" if there are no products in there, I would like to make that happen with an if/else statement.

what does it say?

Mine says empty cart

Mine showed nothing at all. I will check again tonight when I get back to it. I want to make a couple different copies with different colors, or incorporate a color picker solution in the admin area for people. That would be cool. If anyone would like to help out we all have been asking for this for years...

samlf3rd

I need a PHP pro here to get an IF Else statement going that would display the number 0 if there are no items in the cart, else should show the Cart with number like the image I posted.
Any help?

<?php 
echo  $product['quantity'
?>



I tried:

<?php
if ( $product['quantity'] < 1) {
echo 
"0"
}
else {
echo  
$product['quantity'];
}
?>


Which gave me a white screen in VirtueMart on the front-end...
If we can get an if else statement to do this it would work almost exactly how I need it to with a little CSS.

PRO

its giving you a white screen because you forgot the   ;   

<?php
if ( $product['quantity'] < 1) {
echo "0";
}
else {
echo  $product['quantity'];
}
?>

Tenuw

Hi.

I'm on Virtuemart: 2.0.6a/Joomla: 2.5.6

I've tried:

<?php
if ( $product['quantity'] < 1) {
echo "0 Item(s)";
}
else {
echo  $product['quantity'];
}
?>

in the default.php > mod_virtuemart_cart

It displays '0 Item(s)', but when adding to the cart, it still displays '0 Item(s)'.

Has anyone got a definitive answer?

I'd also like to know how to display 'Item(s)' when more items are added to the cart. I've tried this:

<?php
if ( $product['quantity'] < 1) {
echo "0 Item(s)";
}
else {
echo  $product['quantity']("Item(s)");
}
?>

but I don't know if it works, as the previous code which I adapted from isn't updating the Item(s).

Many thanks.