Documentation

Function that tells us if the items in Shopping Cart / Basket

<?php
// Provided by stevetook in the forums.
function items_on_cart() {
  global
$user;
 
$cart = commerce_cart_order_load($user->uid);
 
$line_items = count($cart->commerce_line_items) ? TRUE : FALSE;
  return
$line_items;
}
?>

Comments

djkentuckyham on November 12, 2013

Question: If I wanted to use in top right header to show items in cart. I.e.: "Your Cart (n)". How would I use the above? Or could I use the above. I would think a PHP block, but because I am a PHP newbie I can't seem to print the $line_items value to screen. Or am I totally off base with intended usage?

swensor swensor on September 23, 2015

Hello, the function in the original thread only checks if there are ANY items in the cart. If there are, it returns true, otherwise it returns false. You would however use similar code to get the # and a sub-total. Here's a quick stab at such a solution:

<?php
   
global $user;
   
   
$cart = commerce_cart_order_load($user->uid);
   
$total = $cart->commerce_order_total["und"][0]["amount"];
   
$total_pretty = commerce_currency_format($total,"USD");
   
$count = count($cart->commerce_line_items);
   
    echo
"There are ".$count." item(s) for a total of ".$total_pretty;
?>

Here's a verbose, well-commented version of the same code, in case you don't know what the hell is going on here. Cheers

<?php
   
   
//loads the 'global' $user object, which Drupal has already loaded elsewhere. 'global' is necessary to avoid making a new, empty $user object. This is a good example of variable scoping
   
global $user;
   
   
//loads the users current cart. You could do this for any user. For new/anonymous users, the value 0 (or no value) will load the current session's cart.
   
$cart = commerce_cart_order_load($user->uid);
   
   
//That's right, both lines above could be replaced with just '$cart = commerce_cart_order_load();' since that will default to the current user. LOL.

    //this gives you the 'raw' integer value of the order's total price. Naturally, it looks like sh**.
   
$total = $cart->commerce_order_total["und"][0]["amount"];
   
//Note: "und" is the language, which is undefined for many drupal sites. Best practice would call using $node->language (eg, $cart->commerce_order_total[$node->language]) or use an entity metadata wrapper instead, but ain't nobody got time for that!
    //Also, that only works depending on context like if you're in a node.tpl.php, etc. etc. Just don't worry about it and go get yourself a cold pop.

    //this formats the value nicely, and is overly complicated in true DC fashion. The 2nd arg (I chose "USD") is required, and accepts strings like "USD" or "EUR" or "GBP"
   
$total_pretty = commerce_currency_format($total,"USD");
   
   
//the simplest line. count an array. like a normal person
   
$count= count($cart->commerce_line_items);
   
   
//ok this is the simplest. but hey it tried.
   
echo "There are ".$count." item(s) for a total of ".$total_pretty;
?>