How to quickly override/modify core block file?
-
12-12-2019 - |
Question
I need to modify 1 line of code in:
Mage/Catalog/Search/Block/Autocomplete.php
$html .= '<li title="'.$this->escapeHtml($item['title']).'" class="'.$item['row_class'].'">'
. '<span class="amount">'.$item['num_of_results'].'</span>'.$this->escapeHtml($item['title']).'</li>';
What would be the best approach to do this? Do I need to override the whole controller(Mage/Catalog/Search/controllers/AjaxController.php) that calls this block or is there any simpler way?
It seems to me that creating a module just to override one line is not right.
Thanks :).
Solution
You need to override this block in the local
Step 1: create Yourpackagename_CatalogSearch.xml under app/etc/modules
<?xml version="1.0" ?>
<config>
<modules>
<Yourpackagename_CatalogSearch>
<active>true</active>
<codePool>local</codePool>
</Yourpackagename_CatalogSearch>
</modules>
</config>
Step 2: Now create Yourpackage folder under app/code/local/ and create CatalogSearch folder under Yourpackage.now create etc/config.xml folder under app/code/local/etc/config.xml
<?xml version="1.0" ?>
<config>
<modules>
<Yourpackagename_CatalogSearch>
<version>0.0.1</version>
</Yourpackagename_CatalogSearch>
</modules>
<global>
<blocks>
<catalogsearch>
<rewrite>
<autocomplete>Yourpackage_CatalogSearch_Block_Autocomplete</autocomplete>
</rewrite>
</catalogsearch>
</blocks>
</global>
</config>
Step 3: Now create Autocomplete.php under the app/code/local/Yourpackage/CatalogSearch/Block/ with the following code
<?php
class Yourpackage_CatalogSearch_Block_Autocomplete extends Mage_CatalogSearch_Block_Autocomplete
{
protected function _toHtml(){
// u can add ur code changes here
}
}