blog site of branko ajzele, senior developer / project manager

Add breadcrumbs in Magento to pages that don’t have them

Here is one solution on how to add breadcrumbs to pages in Magento that by default do not have breadcrumbs. Recently we had a client that requested a basic breadcrumb to be shown on pages that Magento does not serve breadcrumbs.

<?php 
/**
 *
 * CUSTOM BREADCRUMBS
 * by Branko Ajzele @surgeworks.com
 *
 * Adds url breadcrumbs for pages that do not have breadcrumbs by default
 *
 */
 
?>
<?php if(is_null($crumbs)): ?>
<?php 
 
/**
 * NOTE
 * On some servers use ->getServer('PATH_INFO')
 * and on some ->getServer('ORIG_PATH_INFO')
 */
 
$urlRequest = Mage::app()->getFrontController()->getRequest();
$urlPart = $urlRequest->getServer('ORIG_PATH_INFO');
 
if(is_null($urlPart))
{
    $urlPart = $urlRequest->getServer('PATH_INFO');
}
 
 
$urlPart = substr($urlPart, 1 );
$currentUrl = $this->getUrl($urlPart);
 
//$controllerName = Mage::app()->getFrontController()->getRequest()->getControllerName();
//$controllerName = ucfirst($controllerName);
 
$controllerName = str_replace("/", " ", $urlPart);
$controllerName = str_replace("_", " ", $controllerName);
$controllerName = str_replace("-", " ", $controllerName);
$controllerName = ucfirst($controllerName);
 
?>
<span class="breadcrumbs">
<strong class="float"><?php echo $this->__("You're currently on: ") ?></strong>
<ul class="breadcrumbs">
<li class="home">
        <a title="<?php echo $this->__('Go to Home Page') ?>" href="<?php echo $this->getUrl() ?>"><?php echo $this->__('Home') ?></a>
    </li>
    <li> / </li>
        <li class="<?php echo strtolower($controllerName) ?>">
        <strong><?php echo $this->__($controllerName) ?></strong>
    </li>
</ul>
</span>
<?php endif; /* END OF CUSTOM BREADCRUMBS */ ?>
  • This was helpful to me for adding breadcrumbs: http://www.magentocommerce.com/boards/viewthrea...
  • Just add :
    Mage::getBlockSingleton('catalog/breadcrumbs')

    at the top of your breadcrumbs file
    \template\page\html\breadcrumbs.phtml

    Now, you can call your breadcrumbs everywhere you want !
  • Hi branko, thx for your advise

    i just have many question about magento

    but first of all , the breadcrumb,

    i have a custom module named Forum, and with controller named indexController and with 2 action, index action and add action

    therefore if i want to have the breadcumb in my custom phtml,
    like home/forum

    what can i do
  • michael
    Just found another shorter variant:
    if (is_null($crumbs)) {
    $headBlock = $this->getLayout()->getblock('head');
    $title = $headBlock->getTitle();
    // Build breadcrumb navigation
    $crumbs['home'] = array(
    'label' => $this->__('Home'),
    'link' => Mage::getBaseUrl(),
    'title' => null,
    'first' => true,
    'last' => null,
    'readonly' => null,
    );
    $crumbs['page'] = array(
    'label' => $title,
    'link' => null,
    'title' => null,
    'first' => null,
    'last' => null,
    'readonly' => null,
    );
    }
  • michael
    Thanks, was exactly, what I was looking for. I changed it so that it uses the (in my case already altered) existing output loop. Also it didn't work with $urlRequest->getServer('ORIG_PATH_INFO');:

    if (is_null($crumbs)) {
    $urlRequest = Mage::app()->getFrontController()->getRequest();
    $urlPart = $urlRequest->getOriginalPathInfo();

    if(is_null($urlPart))
    {
    $urlPart = $urlRequest->getPathInfo();
    }

    $urlPart = substr($urlPart, 1);
    $currentUrl = $this->getUrl($urlPart);

    $controllerName = str_replace("/", " ", $urlPart);
    $controllerName = str_replace("_", " ", $controllerName);
    $controllerName = str_replace("-", " ", $controllerName);
    $controllerName = ucfirst($controllerName);

    // Build breadcrumb navigation
    $crumbs['home'] = array(
    'label' => $this->__('Home'),
    'link' => Mage::getBaseUrl(),
    'title' => null,
    'first' => true,
    'last' => null,
    'readonly' => null,
    );
    $crumbs['page'] = array(
    'label' => $this->__($controllerName),
    'link' => null,
    'title' => null,
    'first' => null,
    'last' => null,
    'readonly' => null,
    );
    }
  • @thomas

    <?php echo $this-?>__('Some string here') ?> plus the right CSV file
  • thomas
    woa this was a fast answer ^^
    na dont worry all is allright with my boss =) but trully all i know about magento dev it's by you ! so again u rock !

    i am curently working on your breadscrumbs mod
    i was wondering how may i translate $controllerName in french

    __($controllerName) ?>

    this dont seems to work maybe it refer to a CSV translation file that doesn't exist in my french package ,any clue ?
  • @thomas

    I'm glad you find my articles useful. About the "boss didn’t fired me yet" part... LOL... :) hope it's not that serious.

    Cheers...
  • thomas
    hey branko,

    just to THANKS you, for all your post about magento !! you kick ass man. my boss didn't fired me yet and its all thanks to you !
  • Hi Pablo, hi Mike... I'm sorry for slow reply, I'm really busy working on projects.

    Pablo, I'm not entirely sure what you have in mind by "come back" link. What if the visitor (customer) lands to some product or catalog list page directly from let's say Google? Will "come back" behave like regular back button or like crumb. I believe you already sorted this out. However, I would like to hear from you what you came up with.

    Mike, about your "Is it possible to implement getPriceHtml (concerning bestsellers on home page)?" question. I have no idea. I generally use the base price that can be read from product object. Then I use Magento helper functions to get special price and so on.
  • Mike
    Hello Branko!

    Offtopic: Would you give some explanation on your bestesellers module?
    Is it possible to implement getPriceHtml (concerning bestsellers on home page)?
    Cannot make it work

    All made following your topic - http://inchoo.net/ecommerce/magento/bestseller-...
  • Ok Branko, I see now.

    I also need to change my breadcrumbs. I want to update my breadcrumbs layout in product page, cart page, account page and some more.

    Instead of the normal breadcrumbs, I'm trying to put a "come back" link. I tried with the same link located in:

    app/design/frontend/default/default/template/customer/account/link/back.phtml

    But it didn't work, because the function is out of customer module (I think, I have not enough programming skill).

    Do you think it can be easy to implement?

    Thank you.
  • I believe that's the right result... I know it might look silly, to have breadcrumb like Home / Index.php (although you should cut off the .php part) but some clients request breadcrumbs like these.

    Same goes if you visi let's say Cart page...

    Cheers...
  • Hi Branko!

    I've tried your code (with ORIG_PATH_INFO and PATH_INFO, both) and I get that:

    You're currently on:
    Home / Index.php

    What do you think?
blog comments powered by Disqus
Powered by Wordpress | Designed by Elegant Themes