VirtueMart Forum

VirtueMart 2 + 3 + 4 => General Questions => Topic started by: samlf3rd on March 26, 2013, 14:20:33 PM

Title: How to display number of products in cart?
Post by: samlf3rd on March 26, 2013, 14:20:33 PM
*****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 (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 (http://www.samswebextensions.com)
Title: Re: How to display number of products in cart?
Post by: K&K media production 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
Title: Re: How to display number of products in cart?
Post by: samlf3rd on March 26, 2013, 22:50:05 PM
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
Title: Re: How to display number of products in cart?
Post by: 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
Title: Re: How to display number of products in cart?
Post by: samlf3rd on March 27, 2013, 03:36:50 AM
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?
Title: Re: How to display number of products in cart?
Post by: samlf3rd on March 27, 2013, 06:39:47 AM
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.
Title: Re: How to display number of products in cart?
Post by: samlf3rd on March 27, 2013, 07:10:13 AM
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.
Title: Re: How to display number of products in cart?
Post by: PRO on March 27, 2013, 12:22:44 PM
what does it look like?

post a picture?

Title: Re: How to display number of products in cart?
Post by: samlf3rd on March 27, 2013, 15:33:24 PM
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]
Title: Re: How to display number of products in cart?
Post by: 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.
Title: Re: How to display number of products in cart?
Post by: 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
Title: Re: How to display number of products in cart?
Post by: samlf3rd on March 27, 2013, 20:16:19 PM
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...
Title: Re: How to display number of products in cart?
Post by: samlf3rd on March 28, 2013, 02:38:14 AM
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.
Title: Re: How to display number of products in cart?
Post by: PRO on March 28, 2013, 16:08:51 PM
its giving you a white screen because you forgot the   ;   

<?php
if ( $product['quantity'] < 1) {
echo "0";
}
else {
echo  $product['quantity'];
}
?>
Title: Re: How to display number of products in cart?
Post by: Tenuw on May 01, 2013, 22:10:56 PM
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.
Title: Re: How to display number of products in cart?
Post by: PRO on May 01, 2013, 22:23:20 PM
Quote from: Tenuw on May 01, 2013, 22:10:56 PM
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.

The javascript file vmprices.js    is what updates this,.


what happens when you update the page?

does it change?
Title: Re: How to display number of products in cart?
Post by: Tenuw on May 02, 2013, 11:52:07 AM
Hi Pro

Thanks for your reply.

I don't know enough about Javascript to convert the PHP code into something meaningful for vmprices.js to function the way I'd like to. Any advice?

Many thanks.

Kind regards.

Title: Re: How to display number of products in cart?
Post by: PRO on May 02, 2013, 19:15:03 PM
whats wrong with the current version of the module?

what do you want it to be different like?

Title: Re: How to display number of products in cart?
Post by: Tenuw on May 03, 2013, 11:48:58 AM
Hi Pro

Thanks for your reply.

I just need less information than whats normally in the cart. Please see the attachment example.

I thought the original PHP code would achieve what I'm after. But as you recommended looking at vmprices.js, I don't actually know how to code javascript for this purpose. If you can help, great. If you can't, I understand.

Many thanks.

Kind regards.

[attachment cleanup by admin]
Title: Re: How to display number of products in cart?
Post by: PRO on May 10, 2013, 14:08:21 PM
look at my site

& add an item to the cart

this is my whole file for

mod_virtuemart_cart/tmpl/default.php


<?php 
//no direct access
defined('_JEXEC') or die('Restricted access');
?>
<div class="vmCartModule" id="vmCartModule"><div id="carticon" class="inline"></div><div class="total_products width59 inline"><?php echo  $data->totalProductTxt ?></div><div class="show_cart floatRight width40 bold"><?php if ($data->totalProduct) echo '<div class="floatRight">'. $data->cart_show .'</div>';?></div><div class="wrap"></div></div>
Title: Re: How to display number of products in cart?
Post by: Tenuw on May 10, 2013, 20:42:28 PM
Hi Pro

Thanks for your reply.

I tried your code. It worked, thank you!

Many thanks, and good day to you!

Kind regards.