12/28/2011

How to override catalogsearch model class in magento

In magento if you wish to override some controller then definately you will get lots of site which will describe how to do that but for model class, you will have some result. So I think to share my knowledge here. I am going to override default catalog search Model of magento. For that I will create an module with name space name Mage and module name Searchby. I hope you people know how to create an module so I will show you how to override model class only

Go to config.xml of your custom module then write the below code to override the catalogsearch model class

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <Mage_Searchby>
            <version>0.1.0</version>
        </Mage_Searchby>
    </modules>
    <frontend>
        <routers>
            <searchby>
                <use>standard</use>
                <args>
                    <module>Mage_Searchby</module>
                    <frontName>searchby</frontName>
                </args>
            </searchby>
        </routers>        
    </frontend>
    <global>
        <models>
            <catalogsearch>
                <rewrite>
                    <layer>Mage_Searchby_Model_Layer</layer>
                </rewrite>
            </catalogsearch>
        </models>
    </global>
</config>

Now create an Layer.php file inside yourmodulename/Model then write the below code

<?php
class Mage_Searchby_Model_Layer extends Mage_CatalogSearch_Model_Layer
{
    public function prepareProductCollection($collection)
    {
        //Code to be executed
    }
}?>

That's all thanks

No comments:

Post a Comment