1

Hi I'm trying to change my current search result page title. So I find the best way is override the file app/code/core/Mage/CatalogSearch/Block/Result.php method protected function _prepareLayout().

So I created a module in app/code/local with the following structure. Please note that this module does other things like post a message to our ERP on a new order or invoice.

app/code/local/Agvalue/Skambau/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <Agvalue_Skambau>
            <version>1.0</version>
        </Agvalue_Skambau>
    </modules>
    <global>
        <models>
            <agvalue_skambau>
                <class>Agvalue_Skambau_Model</class>
            </agvalue_skambau>
        </models>
        <events>
            <sales_order_place_after>
                <observers>
                    <agvalue_skambau_order_observer>
                        <type>model</type>
                        <class>agvalue_skambau/observer</class>
                        <method>skambauCreateOrder</method>
                    </agvalue_skambau_order_observer>
                </observers>
            </sales_order_place_after>
            <sales_order_invoice_save_after>
                <observers>
                    <agvalue_skambau_invoice_observer>
                        <class>agvalue_skambau/observer</class>
                        <method>skambauCreateInvoice</method>
                    </agvalue_skambau_invoice_observer>
                </observers>
            </sales_order_invoice_save_after>
        </events>
        <blocks>
            <catalogsearch>
                <rewrite>
                    <result>Agvalue_Skambau_Block_Result</result>
                </rewrite>
            </catalogsearch>
        </blocks>
    </global>
</config>

app/code/local/Agvalue/Skambau/Block/Result.php

<?php
class Agvalue_Skambau_Block_Result extends Mage_CatalogSearch_Block_Result
{

    protected function _prepareLayout()
    {
        // add Home breadcrumb
        $breadcrumbs = $this->getLayout()->getBlock('breadcrumbs');
        if ($breadcrumbs) {
            $title = $this->__("Search results for: '%s'", $this->helper('catalogsearch')->getQueryText());
            $breadcrumbs->addCrumb('home', array(
                'label' => $this->__('Home'),
                'title' => $this->__('Go to Home Page'),
                'link'  => Mage::getBaseUrl()
            ))->addCrumb('search', array(
                'label' => $title,
                'title' => $title
            ));
        }
        // modify page title
        $title = $this->helper('catalogsearch')->getEscapedQueryText();
        $this->getLayout()->getBlock('head')->setTitle($title);

        return parent::_prepareLayout();
    }
}

app/etc/modules/Agvalue_Skambau.xml

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

There is an opportunity here to use an observer instead of overriding the original method. It’s considered a better practice to use that approach when possible. Here is how you could do it:

app/code/local/Agvalue/Skambau/etc/config.xml

Under the config/frontend/events node:

<core_block_abstract_prepare_layout_after>
    <observers>
        <updateSearchResultsTitle>
            <class>Agvalue_Skambau_Model_Observer</class>
            <method>updateSearchResultsTitle</method>
        </updateSearchResultsTitle>
    </observers>
</core_block_abstract_prepare_layout_after>

app/code/local/Agvalue/Skambau/Model/Observer.php

class Agvalue_Skambau_Model_Observer
{
    public function updateSearchResultsTitle(Varien_Event_Observer $observer)
    {
        $block = $observer->getBlock();
        if ($block instanceof Mage_CatalogSearch_Block_Result) {
            $title = Mage::helper('catalogsearch')->getEscapedQueryText();
            $block->getLayout()->getBlock('head')->setTitle($title);
        }
        return $this;
    }
}
  • Hi @fantasticrice, it worked very well. Thanks for your help. – André Andrade Mar 29 '17 at 19:50

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.