6 replies [Last post]
volocuga's picture
Offline
Joined: 02/09/2009
Juice: 68
Was this information Helpful?

I need to display number of item and total amount in block to put anywhere.

For example:

Your cart:item(number)amount $(number)

I ve tried 3 similar snippets:

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

and

<?php
$number_of_items
= count(uc_cart_get_contents());
?>

and

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

but block shows nothing.PHP input enabled.
Any tips on this?

vincew's picture
Offline
Joined: 01/21/2009
Juice: 153
Try this: <?php  $total =

Try this:

<?php
  $total
= 0;

 

// Load the current shopping cart.
 
$items = uc_cart_get_contents();

 

// Count the number of items
 
$number_of_items = count($items);

 

// Multiply price and qty for each item
 
foreach( $items as $item ){
   
$total += ($item->qty * $item->price);
  }
?>

More documentation on this is here: http://www.ubercart.org/docs/api/uc_cart_get_contents

Best
VinceW

-=[ Your Information Matters ]=-

(You may use my personal contact form to discuss drupal/ubercart work.)

volocuga's picture
Offline
Joined: 02/09/2009
Juice: 68
Re: Cart item count and total price snippet

I forgot to add "print" Smiling
Here is correct code:

<?php
  $subtotal
= 0;
  foreach (
uc_cart_get_contents() as $item) {
   
$subtotal += $item->price * $item->qty;
  }
print
$subtotal;
?>
<?php
  $items
= 0;
  foreach (
uc_cart_get_contents() as $item) {
   
$items += $item->qty;
  }
print
$items;
?>
shv_rk's picture
Offline
Joined: 08/16/2010
Juice: 112
Hi there, I am using this

Hi there,

I am using this code snippet in my cart block summary to show only total amount.
how can I make it to show currency sign and in decimal?
now it shows like:

Total amount = 40

but i want it to be:

Total amount: £40.00

KingAndy's picture
Offline
Joined: 04/01/2009
Juice: 95
uc_price

You're going to have to use uc_price on the final total. It's slightly more complicated than it sounds - you need to give it a "context" as well as a price. In this case the context is going to be array('revision' => 'themed', 'type' => 'amount'). So output it like so:

<?php
  $context
= array('revision' => 'themed', 'type'  => 'amount');
  print
uc_price($subtotal, $context);
?>
KingAndy's picture
Offline
Joined: 04/01/2009
Juice: 95
uc_price

The QTY calculation here is fine. Unfortunately uc_cart_get_contents doesn't take into account any price alterations that may be taking place, so we'll need to call uc_price() so that the price alterers can step in and make any changes they need to make ... having had a look at theme_cart_review_table() it looks like we need to pass the whole cart as well as the individual cart item through into the uc_price() call. Which is fun, of course. Then you can run the whole thing through uc_price again to get it themed (or just formatted, if you prefer - themed potentially has extra HTML) for final display.

The below calculation is largely cribbed from theme_cart_review_table():

<?php
  $contents
= uc_cart_get_contents();
  foreach (
$contents as $item) {
   
   
$price_info = array(
     
'price' => $item->price,
     
'qty' => $item->qty,
    );
   
   
$context['revision'] = 'altered';
   
$context['type'] = 'cart_item';
   
$context['subject'] = array(
     
'cart' => $contents,
     
'cart_item' => $item,
     
'node' => node_load($item->nid),
    );

   

$subtotal += uc_price($price_info, $context);
  }
 
 
$context = array('revision' => 'themed', 'type'  => 'amount');
  print
uc_price($subtotal, $context);
?>
ryanschmidt's picture
Offline
Joined: 11/17/2008
Juice: 235
Re: Cart item count and total price snippet

Has anyone used something like this to add the count to the menu? For example, I have a "My Cart" link in the menu but would like it to be "My Cart (3)" based on the items they have.