4

In my site I need to display breadcrumbs like this.

Home / Search results for: 'laptop' / Lenova G50 Laptop

I am searching any products (like above "laptop") in site.

After get result I click any products in search result page.

That clicked product view page I need above type of breadcrumbs.

When I click that " Search results for: 'laptop' ", go to that search result page.

How can i do this ?

Any one help me.

Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352
VijayS91
  • 1,788
  • 1
  • 20
  • 41

2 Answers2

3

The observer way

On top of what Raul said and instead of rewriting the entire breadcrumbs block you could use an observer

Create app/code/local/Vendor/Module/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Vendor_Module>
      <version>0.0.1</version>
    </Vendor_Module>
  </modules>
  <global>
        <models>  
            <module>
                <class>Vendor_Module_Model</class>
            </module>
        </models>
  </global>
  <frontend>
        <events>
                <core_block_abstract_prepare_layout_before>
                    <observers>
                        <vendor_module_core_block_abstract_prepare_layout_before>
                            <type>model</type>
                            <class>module/observer</class>
                            <method>addSearchBreadcrumbs</method>
                        </vendor_module_core_block_abstract_prepare_layout_before>
                    </observers>
                </core_block_abstract_prepare_layout_before>
        </events>
    </frontend> 
</config> 

Then create your observer app/code/local/Vendor/Module/Model/Observer.php

<?php

class Vendor_Module_Model_Observer {
    public function addSearchBreadcrumbs(Varien_Event_Observer $observer)
    {
         $block = $observer->getEvent()->getBlock();
         if ($block instanceof Mage_Catalog_Block_Breadcrumbs) {
             if ($product = Mage::registry('current_product')){

                 // check referer url to see if you come from a search result page
                 // get your stored in session search string, and include it
                 $block->addCrumb('XXXX', array(
                     'label'=> XXXX
                     'title'=> XXXX
                     'link'=> XXXX
                 ));
             }
         }
    }
}
Raphael at Digital Pianism
  • 70,385
  • 34
  • 188
  • 352
2

You'd want to take a look to Mage_Catalog_Block_Breadcrumbs class, catalog breadcrumbs path is built in _prepareLayout() method, you can compare the code with same method in Mage_CatalogSearch_Block_Result class

One way to achieve this could be:

Rewrite Mage_CatalogSearch_Block_Result class, so you can store in session label, title & url of the search result page to use it later

Rewrite Mage_Catalog_Block_Breadcrumbs class, and then check referer url (Reliable way to redirect to last page), if it's a search result url then you should modify breadcrumbs path, to include the stored in session search path

Something like this...

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()
        ));

        if ($product = Mage::registry('current_product')){

            // check referer url to see if you come from a search result page
            // get your stored in session search string, and include it
            $breadcrumbsBlock->addCrumb('XXXX', array(
                'label'=> XXXX
                'title'=> XXXX
                'link'=> XXXX
            ));
        }
Raul Sanchez
  • 3,036
  • 3
  • 28
  • 63