Metal Toad has been building applications and cloud environments for some of the most well-known global brands for over a decade. Learn more > >

More complete breadcrumbs for Ubercart checkout

By default, Ubercart sets the breadcrumb on the checkout page to simply "Home", which I personally find a bit odd. Because it calls drupal_set_breadcrumb() late in the request cycle, it's not even possible to create menu links for use by the menu_breadcrumb or menutrails modules. Stranger still, the cart settings page offers a "Custom cart breadcrumb" text and URL option, but it's hard-coded to use a single link instead of a trail of links.

Here is a small snippet that will set the breadcrumbs to mimic the URL paths, for example:

Home › Cart › Checkout

/**
 * Implementation of hook_preprocess_page().
 */
function mymodule_preprocess_page(&$variables) {
  /*
   * This section sets the breadcrumb on the cart pages hierarchically
   * according to the path elements.  A preprocess function is
   * needed instead of drupal_set_breadcrumb() because Ubercart
   * aleady calls drupal_set_breadcrumb() with undesirable results,
   * which also thwarts the menutrails module.
   */
  $menu = menu_get_item();
  if (preg_match('/^cart($|\/)/', $menu['path'])) {
    $crumbs = array(l(t('Home'), '<front>'));
    $paths = array();
    foreach ($menu['map'] as $arg) {
      $paths[] = $arg;
      $crumbs[] = l(ucfirst($arg), implode('/', $paths));
    }
   $variables['breadcrumb'] = theme('breadcrumb', $crumbs);
  }
}
Filed under:

Comments

The default checkout process in D7 UC3 still looks to work in the same way i.e. the checkout page is just Home.

I am new to this level of Drupal. So to implement the above, does this need to be placed into a module, or is this code for template.php?

Is there anything obvious that needs to change for D7 UC3?

Just a tip that stumped me for an hour or so. Make sure the weight of the module you put your code snippet into is heavier than uc_cart.

Thanks for the snippet!

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.

Ready to get started?