Magento 1.7 – How to Remove Part of Breadcrumb from Category and Product Page

breadcrumbscatalogmagento-1.7product

I added an extra category but i don't want to show this in the breadcrumb.

I searched for a while how i can remove a part from it and found the following solution:

In app/code/local/Mage/Page/Block/Html/Breadcrumbs.php I added the following code:

function removeCrumb($crumbName) {
    if ((!isset($this->_crumbs[$crumbName])) || (!$this->_crumbs[$crumbName]['readonly'])) {
    unset($this->_crumbs[$crumbName]);
}
    return $this;
}

Then ,i added the following code in app/code/local/Mage/Catalog/Block/Product/View.php after

$this->getLayout()->createBlock(‘catalog/breadcrumbs’);

$this->getLayout()->getBlock('breadcrumbs')->removeCrumb('home');

Instead of 'home' I can use the word category + the id of the category to remove this part from the breadcrumbs.

This all works fine, but unfortunately only on the product page. When I try to do the same in app/code/local/Mage/Catalog/Block/Category/View.php something strange happens:

instead of 'home - cat1 - cat2' it becomes 'home - cat2 - cat1'.

No idea why.

Does someone knows how to solve this problem for the category page?

I'm using magento 1.7.0.2

Best Answer

It would be wiser to rewrite the Mage_Catalog_Helper_Data, as it provides the data on what categories will be added to breadcrumbs.

Assuming you know how to rewrite that helper class, here's what you have to do:

  1. Rewrite the class Mage_Catalog_Helper_Data.
  2. Override getBreadcrumbPath method.
  3. Copy the original method but modify these lines:

    // add category path breadcrumb
    foreach ($pathIds as $categoryId) {
        if (isset($categories[$categoryId]) && 
            $categories[$categoryId]->getName() &&
            $categoryId != $YOUR_CATEGORY_ID // Notice the extra category check here...
        ) {
            $path['category'.$categoryId] = array(
                'label' => $categories[$categoryId]->getName(),
                'link' => $this->_isCategoryLink($categoryId) ? $categories[$categoryId]->getUrl() : ''
            );
        }
    }
    

Replace $YOUR_CATEGORY_ID variable or set a value for it.

EDIT:

In case you don't know how to do a class rewrite

  1. Open the file Mage_Catalog_Helper_Data.
  2. Find the method getBreadcrumbPath. It starts with something like public function getBreadcrumbPath()
  3. Replace the whole method with this code:

    public function getBreadcrumbPath()
    {
        if (!$this->_categoryPath) {
    
            $path = array();
            if ($category = $this->getCategory()) {
                $pathInStore = $category->getPathInStore();
                $pathIds = array_reverse(explode(',', $pathInStore));
    
                $categories = $category->getParentCategories();
    
                !$category_ids_to_remove = array(YOUR_CATEGORY_ID_1, YOUR_CATEGORY_ID_2);
                // add category path breadcrumb
                foreach ($pathIds as $categoryId) {
                    if (isset($categories[$categoryId]) && 
                        $categories[$categoryId]->getName() &&
                        !in_array($categoryId, $category_ids_to_remove) // Notice the extra category check here...
                    ) {
                        $path['category'.$categoryId] = array(
                            'label' => $categories[$categoryId]->getName(),
                            'link' => $this->_isCategoryLink($categoryId) ? $categories[$categoryId]->getUrl() : ''
                        );
                    }
                }
            }
    
            if ($this->getProduct()) {
                $path['product'] = array('label'=>$this->getProduct()->getName());
            }
    
            $this->_categoryPath = $path;
        }
        return $this->_categoryPath;
    }
    

Replace the text YOUR_CATEGORY_ID to the actual category ID of the category you want to hide. It should work after that.

Please take note that rewrite core classes is extremely not recommended as core files are replaced whenever you update your magento.

Related Topic