hi

i have got a code for custom cart block. its giving a perfect output like this " 0 items 0.00 " but i want to format it somewhat like this " Cart: 0 items ($0.00) " and hyperlinked. here is the code..

<?php
$number_of_items = count(uc_cart_get_contents());
echo $number_of_items;
?>
<?php
   $sinitem = "item";
   $pluitems = "items";

if ($number_of_items == 1) {
   echo $sinitem;
}  else {
   echo $pluitems;
}
?>

<?php
  $subtotal = 0;
  foreach (uc_cart_get_contents() as $item) {
    $subtotal += $item->price * $item->qty;
  }
?>

<?php
$dtotal = sprintf("%1\$.2f",$subtotal);
echo $dtotal;
?>

check this link to understand what i want.. http://www.ubercart.org/files/rifftrax_.jpg

Comments

nevets’s picture

This should help, you will need to determine where to link to

<?php
$output =  'Cart: ';

$number_of_items = count(uc_cart_get_contents());
$output  .= $number_of_items;

   $sinitem = "item";
   $pluitems = "items";

if ($number_of_items == 1) {
   $output  .= $sinitem;
}  else {
   $output  .= $pluitems;
}

  $subtotal = 0;
  foreach (uc_cart_get_contents() as $item) {
    $subtotal += $item->price * $item->qty;

$dtotal = sprintf("%1\$.2f",$subtotal);
$output  .=  ' ($' . $dtotal . ')';
//You need  to set path  approriately
//Should be a valid Drupal path
$path =  'node/1';
print l($output, $path);
?>
khanz’s picture

thanx for help nevets, i used your code but now it shows a parse error.
:( still looking for the answer.

------------
Volvo, Video, Velcro. (I came, I saw, I stuck around.)

nevets’s picture

What line number?

khanz’s picture

On line 26

this is the exact error i am getting...

Parse error: parse error, unexpected $end in D:\Programs\EasyPHP2\www\drupal6_demo\sites\all\themes\acquia_marina\block-uc_cart.tpl.php on line 26

------------
Volvo, Video, Velcro. (I came, I saw, I stuck around.)

nevets’s picture

If ?> is the last line, simply remove it.

khanz’s picture

removing ?> didnt helped... but placing a closing curly bracket after this part solved my problem..

  $subtotal = 0;
  foreach (uc_cart_get_contents() as $item) {
    $subtotal += $item->price * $item->qty;
}

theres one more minor problem, i want a space between 0 and items "Cart: 0items ($0.00)". btw thanx for improving the snippet.

------------
Volvo, Video, Velcro. (I came, I saw, I stuck around.)

nevets’s picture

Change

$output  .= $number_of_items;

to

$output  .= $number_of_items . ' ';
khanz’s picture

thanx nevets that did the job... its perfect now

------------
Volvo, Video, Velcro. (I came, I saw, I stuck around.)

khanz’s picture

output = 'Cart: ';

How could i place an image in above snippet and remove the text " cart: "

------------
Volvo, Video, Velcro. (I came, I saw, I stuck around.)

gausarts’s picture

For future seeker, added new function: http://drupal.org/node/508888

Then when I need to put a simple count, I will just call a dead simpler one line of code: uc_cart_get_total_qty();

love, light n laughter