A tiny cart block for Ubercart

Here's a tiny shopping cart block for Ubercart that fits right in with the menu. Of course the standard block could be themed this way, but this is handy when the design calls for the full-sized block elsewhere in the layout.

/**
 * Implementation of hook_block().
 */
function uc_microcart_block($op = 'list', $delta = 0, $edit = array()) {
  switch ($op) {
    case 'list':
      $blocks[0] = array(
        'info' => t('Micro-sized cart block for page header.'),
        // This block cannot be cached, because anonymous
        // sessions can have differing cart contents.
        // To improve this, see drupal.org/project/uc_ajax_cart
        'cache' => BLOCK_NO_CACHE,
      );
      return $blocks;
    case 'view':
      if ($item_count = uc_cart_get_total_qty()) {
        $block = array();
        $block['subject'] = '';
        $block['content'] = theme('image',
          drupal_get_path('module', 'uc_cart') .'/images/cart-full.png');
        $block['content'] .= format_plural($item_count,
          'My cart: 1 item', 'My cart: @count items');
        $block['content'] = l($block['content'], 'cart', array('html' => TRUE));
        return $block;
      }
      break;
    }
}
Filed under 

Hi Dylan, It looks great. I've added the code to one of my custom module files but how do I make it appear on my site? The item doesn't show up in my blocks list.

Hi Dylan,

Thank you so much for your example. Here is the D7 version for those interested.

function uc_microcart_block_info() {
    $blocks['microcart'] = array(
        'info' => t('Microcart'),
        // This block cannot be cached, because anonymous
        // sessions can have differing cart contents.
        // To improve this, see drupal.org/project/uc_ajax_cart
        'cache' => DRUPAL_NO_CACHE
    );
 
    return $blocks;
}
 
function uc_microcart_block_view($delta = '') {
    $block = array();
 
    switch ($delta) {
        case 'microcart':
            if ($item_count = uc_cart_get_total_qty()) {
                $block = array();
                $block['subject'] = '';
                $imagePath = drupal_get_path('module', 'uc_cart') .'/images/cart-full.png';
                $linkText = theme('image', array('path' => $imagePath));
                $linkText .= format_plural($item_count, 'My cart: 1 item', 'My cart: @count items');
                //$block['content'] = l($block['content'], 'cart', array('html' => TRUE));
                //$block['content'] = l($linkText, 'cart', array('html' => TRUE));
                $block['content'] = $linkText;
                return $block;
            }
            $block['content'] = 'View Cart';
            break;
    }
    return $block;
}

Correction:

function uc_microcart_block_info() {
    $blocks['microcart'] = array(
        'info' => t('Microcart'),
        // This block cannot be cached, because anonymous
        // sessions can have differing cart contents.
        // To improve this, see drupal.org/project/uc_ajax_cart
        'cache' => DRUPAL_NO_CACHE
    );
 
    return $blocks;
}
 
function uc_microcart_block_view($delta = '') {
    $block = array();
 
    switch ($delta) {
        case 'microcart':
            if ($item_count = uc_cart_get_total_qty()) {
                $block = array();
                $block['subject'] = '';
                $imagePath = drupal_get_path('module', 'uc_cart') .'/images/cart-full.png';
                $linkText = theme('image', array('path' => $imagePath));
                $linkText .= format_plural($item_count, 'My cart: 1 item', 'My cart: @count items');
                $block['content'] = l($linkText, 'cart', array('html' => TRUE));
                return $block;
            }
            break;
    }
    return $block;
}

Add new comment

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <cpp>, <java>, <php>. The supported tag styles are: <foo>, [foo].
  • Web page addresses and email addresses turn into links automatically.
  • Lines and paragraphs break automatically.
By submitting this form, you accept the Mollom privacy policy.

About the Author

Dylan Tack, Director of Technology

Dylan is a software engineer with more than a decade of experience working with a wide variety of clients including the Linux Foundation, PBS, Habitat for Humanity, TV.com and the Emmys. His background includes training as an electrical engineer, but he became passionate about open source through his work with a university genetics lab.

Dylan is a proud member of the Drupal community, a member of the Drupal security team, and has extensive experience with Perl and Java. His other interests include computer security, embedded design, climbing, and brewing.