4

I.e. if you click on a product link in a Google search for example, when you come to the product page it will show the breadcrumb simply as:

Home / Product name

Which is missing the category structure showing where this product is held.

So there is key navigation links missing from this page that is displayed for the user.

I have the below code that will fix this BUT it doesn't work on any sub stores, and completely breaks them.

/**
 * Preparing layout
 *
 * @return Mage_Catalog_Block_Breadcrumbs
 */
protected function _prepareLayout()
{
if ($breadcrumbsBlock = $this->getLayout()->getBlock('breadcrumbs')) {
    $breadcrumbsBlock->addCrumb('home', array(
        'label'=>Mage::helper('catalog')->__('Home'),
        'title'=>Mage::helper('catalog')->__('Go to Home Page'),
        'link'=>Mage::getBaseUrl()
    ));
    // sometimes magento can't get category associated with a product
    // so the full breadcrumb is not shown
    // this is a hack to fix the issue.
    $current_category   = Mage::registry('current_category');
    $current_product    = Mage::registry('current_product');
    // let's check if magento knows what current category is
    // if it doesn't know, let's feed this info to it's brain :)
    if(!$current_category && $current_product){
        $categories = $current_product->getCategoryCollection()->addAttributeToSelect('name')->setPageSize(1);
        foreach($categories as $category) {
            Mage::unregister('current_category');
            Mage::register('current_category', $category);
        }
    }
    $title = array();
    $path  = Mage::helper('catalog')->getBreadcrumbPath();
    foreach ($path as $name => $breadcrumb) {
        $breadcrumbsBlock->addCrumb($name, $breadcrumb);
        $title[] = $breadcrumb['label'];
    }
    if ($headBlock = $this->getLayout()->getBlock('head')) {
        $headBlock->setTitle(join($this->getTitleSeparator(), array_reverse($title)));
    }
}
return parent::_prepareLayout();
}

Any ideas on a full solution that will work with sub stores? My understanding is there are basic SEO benefits of implementing this on top of the navigational benefit.

  • I've found a good article on this - stackoverflow.com/questions/12417499/… but it doesn't quite fully resolve our issue as it doesn't tackle the problem of the code not working for sub stores. I would imagine this code requires some lines added in to make the changes relevant on a store level?? – MagentoMac Oct 30 '13 at 0:08
  • Just a thought: If your product were to be in different categories how would you expect the breadcrumbs to show then? For eg: if you had a product named prd1 and it was inlcluded in 2 categories cat1 and cat2 how would the breadcrumbs show up then? – Shatir Oct 30 '13 at 5:43
6

I know this is quite old, but I'd like to share my solution anyway as it doesn't override/clone any core files.

  1. In your custom module add the following to your config.xml:

    <config>
        ...
        <frontend>
            <events>
                <catalog_controller_product_init>
                    <observers>
                        <breadcrumb_categorypath_product_init>
                            <type>singleton</type>
                            <class><Your Namespace>_<Your Module>_Model_Observer</class>
                            <method>fullBreadcrumbCategoryPath</method>
                        </breadcrumb_categorypath_product_init>
                    </observers>
                </catalog_controller_product_init>
            </events>
        </frontend>
    </config>
    
  2. Create an Observer.php in /app/code/local/<Your Namespace>/<Your Module>/Model/

  3. Add the following to your Observer.php:

    class <Your Namespace>_<Your Module>_Model_Observer {
        public function fullBreadcrumbCategoryPath(Varien_Event_Observer $observer) {
            $current_product = Mage::registry('current_product');
    
            if( $current_product ) {
                $categories = $current_product->getCategoryCollection()->addAttributeToSelect('name')->setPageSize(1);
                foreach( $categories as $category ) {
                    Mage::unregister('current_category');
                    Mage::register('current_category', $category);
                }
            }
        }
    }
    
  4. You should be good to go.

  • is there a way to get the longest possible path e.g. this is a product that is also in this categorys subcategory? – Yehuda Schwartz Jun 6 '16 at 13:56
  • @YehudaSchwartz The code above should be doing the desired already. – oschloebe Jun 6 '16 at 16:18
  • it is not on my site the main category is an anchor and the sub cats are not does that make a diffrence? – Yehuda Schwartz Jun 6 '16 at 17:10
3

There's an extension on magento connect called Exotern Crumbs that I'm using. I'm not using sub stores, so I can't vouch for that. Here's the product description

There is a popular problem among magento-store owners. When customer of your store comes to product page by direct link, he can only see link to homepage and product title in breadcrumbs. And this extension gives ability to configure the type of crumbs to be displayed.

First of all you can configure extension to show only the longest path through categories to the product. Also, if you want, it can display the shortest path. And the most demanded function is to display all available pathes in compact size.

  • do you know how this extension works? either some code or the setup via the admin section, maybe a nice screen shot to show the front end would help. – David Manners Oct 30 '13 at 7:49
  • Hi @Richard this works nicely in our main store, example: galaxystores.co.uk/… and it also works in our sub stores too!!! :) Example - diningsetsgalaxy.co.uk/… So looks like this is the best solution. – MagentoMac Oct 30 '13 at 10:21
  • Although I'm not sure at the moment how I feel about multiple breadcrumbs showing at the top of the page. Just 1 would have been ideal, I fear it will show all of the breadcrumbs for every category a product is in? So if 1 product is in 10 categories, 10 breadcrumbs will show? Researching now anyway, will report back with findings. Thanks for your help thus far, great extension you've found there! :) – MagentoMac Oct 30 '13 at 10:23
  • Ignore that last comment..... if you select in settings "show only one path" to yes, then it will only display 1 breadcrumb. So this extension is perfect! Yippee!! :) – MagentoMac Oct 30 '13 at 10:25
  • Hi David, sorry, the extension is free, there's also a demo site linked from connect. I'll remember next time that code I paste is more permanent. – Richard Oct 30 '13 at 10:44
0

Have you tried to instead use the admin configuration for this?

System->Configuration->Catalog->Catalog->Search Engine Optimization->Use Category Path for Product URL = yes

  • Hi @pspahn no we've not as yet, my understanding of this function is that it simply adds the parent category name into the url path of that product, and is unrelated to the breadcrumb function of the product page? I may be wrong. I'll try it anyway and let you know the outcome. Just re-indexing catalog urls now. – MagentoMac Oct 30 '13 at 0:47
  • Ok, for some reason I was thinking the breadcrumbs are based off the URL rewrite used by the product. – pspahn Oct 30 '13 at 0:53
0

As its not mentioned magento version like M1 or M2. Here is what you need to do for M2. Its kind of like the same as @Alphawolf mentioned but the event is init_after.

Here is the code for events.xml

<event name="catalog_controller_product_init_after">
        <observer name="add_current_category_breadcrumb_after" instance="Vendor\Module\Observer\Addcurrentcategory" />
    </event>

Now create a file named Addcurrentcategory.php inside Vendor\Module\Observer.

namespace Vendor\Module\Observer;

class Addcurrentcategory implements \Magento\Framework\Event\ObserverInterface
{
    protected $_coreRegistry;

    public function __construct(
        \Magento\Framework\Registry $coreRegistry

    ) {
        $this->_coreRegistry = $coreRegistry;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $current_product = $observer->getData('product');
        if( $current_product ) {
            $categories = $current_product->getCategoryCollection()->addAttributeToSelect('name')->addAttributeToSelect('id')->addAttributeToSort('entity_id','DESC')->setPageSize(1);
            foreach( $categories as $category ) {
                $this->_coreRegistry->unregister('current_category');
                $this->_coreRegistry->register('current_category', $category);
            }
        }
    }
}

Now it should always show breadcrumb even if select category path in product url is set to No From Admin->configuration->Catalog->Catalog->Search Engine Optimization.

Your Answer

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

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