3

By Default Manual Navigation Products breadcrumb looks like,

Wanted product detail page:

enter image description here


When I reached to product page using by search the breadcrumb looks like,

Current product detail page:

enter image description here

How can I get Complete Breadcrumb of the desired products even reached by search?

1
+50

You can try this one ... create an extension with this code:

app/etc/modules/My_Module.xml

<?xml version="1.0"?>
<config>
    <modules>
        <My_Module>
            <active>true</active>
            <codePool>local</codePool>
        </My_Module>
    </modules>
</config>

app/code/local/My/Module/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <My_Module>
            <version>0.0.1</version>
        </My_Module>
    </modules>
    <global>
        <models>
            <my_module>
                <class>My_Module_Model</class>
            </my_module>
        </models>
    </global>
    <frontend>
        <events>
            <catalog_controller_product_view>
                <observers>
                    <add_category_to_breadcrumbs>
                      <class>my_module/observer</class>
                      <method>addCategoryToBreadcrumbs</method>
                    </add_category_to_breadcrumbs>
                </observers>
            </catalog_controller_product_view>
        </events>
    </frontend>
</config>

app/code/local/My/Module/Model/Observer.php

class My_Module_Model_Observer extends Mage_Core_Model_Observer
{
    public function addCategoryToBreadcrumbs(Varien_Event_Observer $observer)
    {
        if (Mage::registry('current_category')) {
            return;
        }

        $product = $observer->getProduct();

        $product->setDoNotUseCategoryId(false);
        $categoryIds = $product->getCategoryIds();

        if (count($categoryIds)) {
            $categories = Mage::getModel('catalog/category')->getCollection()
                ->addAttributeToFilter('entity_id', $categoryIds)
                ->addAttributeToFilter('is_active', 1);

            $categories->getSelect()->order('level DESC')->limit(1);

            Mage::register('current_category', $categories->getFirstItem());
        }
    }
}
  • not working, getting the same error – user51361 Jun 12 '17 at 12:30
  • Please flush the cache and check if extension is loaded (add Mage::log('loaded', null, 'test.log', true) to addCategoryToBreadcrumbs before if (Mage::registry('current_category')) – sv3n Jun 12 '17 at 13:48
  • I did exactly what you said, again not working, could you pls join chat chat.stackexchange.com/rooms/60356/… – user51361 Jun 13 '17 at 5:24
  • Its working good,, – user51361 Jun 13 '17 at 7:31
  • 1
    Working fine. great work. – Chandra Bhushan Apr 22 at 6:26
0

The easiest way is to include category URL Keys into your product URLs. The is a special setting in Magento to do this in System > Configuration > Catalog > Search Engine Optimization > Use Categories Path for Product URLs

enter image description here

In case this way is not ok for you, you can use a ready solution like this one: https://www.magestyapps.com/magento-extensions/advanced-breadcrumbs.html

0

You can add this to magento/app/code/core/Mage/Catalog/Helper/Data.php after if ($category = $this->getCategory()) { ... }

        if(empty($path)) {
            if ($this->getProduct()) {

                $collection =  Mage::getModel('catalog/category')->getCollection()
                    ->setStoreId(Mage::app()->getStore()->getId())
                    ->addAttributeToSelect('name')
                    ->addAttributeToSelect('is_active')
                    ->addFieldToFilter('entity_id', array('in' => $this->getProduct()->getCategoryIds()));

                $categories = array();
                $cate = false;
                foreach ($collection as $category) {
                    if($category->getId()) {
                        $cate = $category;
                        $pathInStore = $category->getPathInStore();
                        $pathIds = array_reverse(explode(',', $pathInStore));
                    }
                }
                if($cate) {
                    $categories = $cate->getParentCategories();
                    if($categories) {
                        foreach($pathIds as $categoryId) {
                                $path['category'.$categoryId] = array(
                                    'label' => $categories[$categoryId]->getName(),
                                    'link' => $this->_isCategoryLink($categoryId) ? $categories[$categoryId]->getURL() : ''
                                );
                        }
                    } else {
                        $path['category'] = array('label' => $cate->getName());
                    }
                }
            }
        }
0

Just a comment to the awesome answer of @sv3n. Let's assume we have this category structure

root > category 1 > category 2 > category 3 > category 4

A product of the category 4 can be listed in Category 1, 2, 3 or 4 If the user clicks the product in the category 2 listing the breadcrumb will be

root > category 1 > category 2 > product

So if always the complete product breadcrumb path must be shown a change is needed

        if (Mage::registry('current_category')) {
            return;
        }

for

        if (Mage::registry('current_category')) {
            Mage::unregister('current_category');
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy