1

I need the breadcrumbs to hyperlink the current page i'm in..

For example

Home >> Category >> Product

usually, all parent categories hyperlinked. i wish to have product hyperlinked as well.

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to [email protected] so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magentocommerce.com for more information.
 *
 * @category    Mage
 * @package     Mage_Page
 * @copyright   Copyright (c) 2012 Magento Inc. (http://www.magentocommerce.com)
 * @license     http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Html page block
 *
 * @category   Mage
 * @package    Mage_Page
 * @author      Magento Core Team <[email protected]>
 */
class Mage_Page_Block_Html_Breadcrumbs extends Mage_Core_Block_Template
{
    /**
     * Array of breadcrumbs
     *
     * array(
     *  [$index] => array(
     *                  ['label']
     *                  ['title']
     *                  ['link']
     *                  ['first']
     *                  ['last']
     *              )
     * )
     *
     * @var array
     */
    protected $_crumbs = null;

    function __construct()
    {
        parent::__construct();
        $this->setTemplate('page/html/breadcrumbs.phtml');
    }

    function addCrumb($crumbName, $crumbInfo, $after = false)
    {
        $this->_prepareArray($crumbInfo, array('label', 'title', 'link', 'first', 'last', 'readonly'));
        if ((!isset($this->_crumbs[$crumbName])) || (!$this->_crumbs[$crumbName]['readonly'])) {
           $this->_crumbs[$crumbName] = $crumbInfo;
        }
        return $this;
    }

    protected function _toHtml() {             

   $cat_id = "";

   if (Mage::registry('current_product')) {
      $product_id = Mage::registry('current_product')->getId();
      $obj = Mage::getModel('catalog/product');
      $_product = $obj->load($product_id); // Enter your Product Id in $product_id

      if ($product_id) {
         $categoryIds = $_product->getCategoryIds();
         $cat_id = $categoryIds[0];
      }

      $category = Mage::getModel('catalog/category')->load($cat_id);
      $cat_name = $category->getName();
      $cat_url =  $this->getBaseUrl().$category->getUrlPath();
   }

   if (is_array($this->_crumbs)) {
      reset($this->_crumbs);
      $this->_crumbs[key($this->_crumbs)]['first'] = true;
      end($this->_crumbs);
      $this->_crumbs[key($this->_crumbs)]['last'] = true;
   }

   if($cat_id) {
      $this->_crumbs['category'.$cat_id] = array('label'=>$cat_name, 'title'=>'', 'link'=>$cat_url,'first'=>'','last'=>'','readonly'=>'');
      ksort($this->_crumbs);
      $home = $this->_crumbs['home'];
      unset($this->_crumbs['home']);
      array_unshift($this->_crumbs,$home);
   }

   $this->assign('crumbs', $this->_crumbs);
   return parent::_toHtml();
}
}

Thanks

3 Answers 3

0

You need to modify getBreadcrumbPath() in app/code/core/Mage/Catalog/Helper/Data as links are not added to the breadcrumb array by default.

Do this with a rewrite that would look like

...
<helpers>
    <catalog>
        <rewrite>
            <data>[Vendor]_[ModuleName]_Helper_Catalog_Data</data>
        </rewrite>
    </catalog>
</helpers>
...

in your config.xml and the following would be the rewritten class

class [Vendor]_[ModuleName]_Helper_Catalog_Data extends Mage_Catalog_Helper_Data
{
    public function getBreadcrumbPath()
    {
        parent::getBreadcrumbPath();
        if (array_key_exists('product', $this->_categoryPath)) {
            $this->_categoryPath['product']['link'] = $this->getProduct()->getProductUrl();
        }

        return $this->_categoryPath;
    }
}
0

Observe the proper event, perhaps something similar to (just guessing the event) core_block_abstract_to_html_before, (but perhaps something that would be less often observed) with a custom observer, similar to:

       <core_block_abstract_to_html_before>
            <observers>
                <df2k2_update_breadcrumb>
                    <class>df2k2_page/observer</class>
                    <method>updateBreadcrumbLink</method>
                </df2k2_update_breadcrumb>
            </observers>
        </core_block_abstract_to_html_before>

Observer Class

 public function updateBreadcrumbLink(Varien_Event_Observer $observer)
 {

        $breadcrumbsBlock = $observer->getEvent()->getBlock();

        if ($breadcrumbsBlock instanceof Mage_Page_Block_Html_Breadcrumbs) {
           // Get the block, get the last breadcrumb item in the array
           // and simply check for registry('current_product') or 'current_category',
           // and set the last item's  [`link`] element to either the product's url or the categories URL 
           // example,
          if (is_array($breadcrumbsBlock->_crumbs)) {
              end($breadcrumbsBlock->_crumbs);
              $breadcrumbsBlock->_crumbs[key($breadcrumbsBlock->_crumbs)]['link'] 
                  = Mage::getRegistry('current_product')->getUrl();
               // = Mage::helper('core')->getCurrentUrl();
          }
          $breadcrumbsBlock->assign('crumbs', $breadcrumbsBlock->_crumbs);

        }

        return $this;
  }

Once you set the link part for the last crumb item in the array of crumbs, then it will automatically render the item to include the linked URL.

There is a lot of detail missing with logic and object checking for current_product and category... Essentially, if there exists a registry for current_product, use that... if current_category, use that... otherwise, simply do whatever you want, or nothing at all.

If you don't wish to look up the details of the product/category.. just get the current URL and set the ['link'] element to the current URL.


-1
<?php if($crumbs && is_array($crumbs)): ?>
<div class="breadcrumbs">
  <ul>
    <?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
        <li class="<?php echo $_crumbName ?>">
        <?php if($_crumbInfo['link']): ?>
            <a href="<?php echo $_crumbInfo['link'] ?>" title="<?php echo $this->escapeHtml($_crumbInfo['title']) ?>"><?php echo $this->escapeHtml($_crumbInfo['label']) ?></a>
        <?php elseif($_crumbInfo['last']): ?>
        <?php // i have add link code here as below ?>
            <a href="<?php echo $_crumbInfo['link'] ?>" title="<?php echo $this->escapeHtml($_crumbInfo['title']) ?>"><strong><?php echo $this->escapeHtml($_crumbInfo['label']) ?></strong></a>
        <?php else: ?>
            <?php echo $this->escapeHtml($_crumbInfo['label']) ?>
        <?php endif; ?>
        <?php if(!$_crumbInfo['last']): ?>
            <span>/ </span>
        <?php endif; ?>
        </li>
    <?php endforeach; ?>
</ul>
</div>
<?php endif; ?>

edit above code in

app/design/frontend/[Your-theme]/[Your-package]/template/page/html/breadcrumbs.phtml
2
  • typo: breadcrums.phtml -- breadcrumbs.phtml, yes?
    – df2k2
    Commented Apr 15, 2018 at 7:04
  • yes breadcrums.phtml Commented Apr 24, 2018 at 9:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.