8 replies [Last post]
wuh
wuh's picture
Offline
Joined: 06/06/2009
Juice: 48

Well the subject says it all - I'm trying to figure out how to add a link to my secondary links to 'View Cart'. Simple enough, except that I'd like to also show the number of products the user has added in brackets. For example 'View Cart (2)'

Anyone have any ideas?

Thanks!

wuh
wuh's picture
Offline
Joined: 06/06/2009
Juice: 48
Re: View Cart (x) link showing number of products added?

Does anyone know how to do this? I'm lost.. :/

wuh
wuh's picture
Offline
Joined: 06/06/2009
Juice: 48
Re: Re: View Cart (x) link showing number of products added?

In case this is of interest to anyone else, I found this piece of code which I used in my page template:

<?php
if (module_exists('uc_store')) {
 
$items = uc_cart_get_contents();
  if (
is_array($items)) {
   
$hasItems = count($items);
  }
  else {
   
$hasItems = 0;   
  }
}
?>
Ryan's picture
Offline
Joined: 08/07/2007
Juice: 15422
Re: Re: Re: View Cart (x) link showing number of products added?

You should use this instead:

<?php
if (module_exists('uc_cart')) {
 
$hasItems = 0;

  foreach (

uc_cart_get_contents() as $item) {
   
$hasItems += $item->qty;
  }
}
?>
wuh
wuh's picture
Offline
Joined: 06/06/2009
Juice: 48
Re: Re: Re: Re: View Cart (x) link showing number of products ad

Oh that does look better - thank you Smiling

Francois's picture
Offline
Joined: 08/19/2008
Juice: 387
Exactly what I was looking for.

I just have 1 question. Where do I place this? My "view cart" button is a one of my secondary links which is basically a link name with a path.

If I add this to the text name of the link it just spits out the PHP not the cart #.

--------
'Twas the end of the world, and you didn't even know it.

wuh
wuh's picture
Offline
Joined: 06/06/2009
Juice: 48
Re: Exactly what I was looking for.

You need to output this manually via your page.tpl.php (or similar) file, it's not possible to show the number of items in the cart using Drupal's menu builder.

Jay Matwichuk's picture
Offline
Joined: 02/15/2010
Juice: 171
Re: Re: Exactly what I was looking for.

You can (and should) do this in template.php rather than page.tpl.php

<?php
function THEMENAME_preprocess_page(&$vars)
{
  if (
module_exists('uc_cart')) {
   
$item_count= 0;

    foreach (
     

uc_cart_get_contents() as $item) {
     
$item_count += $item->qty;
    }
  }
$vars['shopping_cart_link'] = l(format_plural($item_count, 'Shopping Cart: !count item', 'Shopping Cart: !count items', array('!count' => $item_count))
}
?>

You can now add the following to page.tpl.php:

<?php
print $shopping_cart_link;
?>
Francois's picture
Offline
Joined: 08/19/2008
Juice: 387
Perfect

Yeah, that's much cleaner. LIking it. I'll be using this for the site I'm working on this month.

EDIT:
The above code did not work for me in D6, UC2. The last line was flunking it out. I changed it as noted, and this works fine.

<?php
function framework_preprocess_page(&$vars) {
 
$vars['tabs2'] = menu_secondary_local_tasks();

  if (

module_exists('uc_cart')) {
   
$item_count= 0;
    foreach (
uc_cart_get_contents() as $item) {
     
$item_count += $item->qty;
    }
  }
  if (
$item_count==0) {
     
$cart_contents = 'Cart Empty';
  }
  elseif (
$item_count==1) {
       
$cart_contents = '1 Item';
  }
  else {
     
$cart_contents = ($item_count . ' Items');
  }  
$vars['shopping_cart_link'] = ('View Cart | ' . $cart_contents);
}
?>

--------
'Twas the end of the world, and you didn't even know it.