2

I'm trying to override the Catalog Search functionality of Magento so that it searches using 'AND' rather than 'OR' for search terms.

The 'correct' way to do this, that is update proof, is to create my own module. So this is what I've done. Unfortunately, this isn't working. The updated method is not being called.

Here's the config file section :

<global> <models> <catalogsearch> <rewrite> <fulltext>MyNameSpace_MyModule_Model_CatalogSearch_Fulltext</fulltext> </rewrite> </catalogsearch> </models> </global> 

The method I'm trying to override is Mage_CatalogSearch_Model_Fulltext, and I'm actually doing an 'extend' - eg,

class MyNameSpace_MyModule_Model_CatalogSearch_Fulltext extends Mage_CatalogSearch_Model_Fulltext { public function prepareResult($object, $queryText, $query) { die("It works..."); } } 

I had read somewhere that there's no point doing this 'overriding' if the class you're actually trying to override is never called with a particular class 'creator' method? If this is true, this may explain why my new class method is never used?

So in that case, how to override this method? Am I doing the right thing here?

3
  • 2
    Your definitions look fine. I noticed you have more params on your overridden function so you should give these default values (Default magento only has $query=null). Have you cleared the cache and checked the module shows up in enabled modules. Any errors in system or exception log? Commented Sep 28, 2013 at 23:18
  • Thanks Ashley - Okay first - the function declaration is just a cut and paste job - there shouldn't be any additional params?! Also - there is nothing in the system or exception logs. Commented Sep 30, 2013 at 14:14
  • Sorry - also have cleared the cached and the module is showing up as enabled Commented Sep 30, 2013 at 14:20

3 Answers 3

3

I had successfully override same functionality look at the code: etc->modules:LevoSoft_CatalogSearch.xml

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

config.xml

<config> <modules> <LevoSoft_CatalogSearch> <version>1.0.0</version> </LevoSoft_CatalogSearch> </modules> <global> <models> <catalogsearch_resource> <rewrite> <fulltext>LevoSoft_CatalogSearch_Model_Resource_Fulltext</fulltext> </rewrite> </catalogsearch_resource> </models> </global> </config>

CatalogSearch->Modal->Resource Fullteext.php

class LevoSoft_CatalogSearch_Model_Resource_Fulltext extends Mage_CatalogSearch_Model_Resource_Fulltext { /** * Prepare results for query with AND operator which will give results as per client's need * * @param Mage_CatalogSearch_Model_Fulltext $object * @param string $queryText * @param Mage_CatalogSearch_Model_Query $query * @return Mage_CatalogSearch_Model_Resource_Fulltext */ public function prepareResult($object, $queryText, $query) { //var_dump("hasaannnnnnnn");exit; $adapter = $this->_getWriteAdapter(); if (!$query->getIsProcessed()) { $searchType = $object->getSearchType($query->getStoreId()); $preparedTerms = Mage::getResourceHelper('catalogsearch') ->prepareTerms($queryText, $query->getMaxQueryWords()); $bind = array(); $like = array(); $likeCond = ''; if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE || $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE ) { $helper = Mage::getResourceHelper('core'); $words = Mage::helper('core/string')->splitWords($queryText, true, $query->getMaxQueryWords()); foreach ($words as $word) { $like[] = $helper->getCILike('s.data_index', $word, array('position' => 'any')); } if ($like) { $likeCond = '(' . join(' AND ', $like) . ')'; } } $mainTableAlias = 's'; $fields = array( 'query_id' => new Zend_Db_Expr($query->getId()), 'product_id', ); $select = $adapter->select() ->from(array($mainTableAlias => $this->getMainTable()), $fields) ->joinInner(array('e' => $this->getTable('catalog/product')), 'e.entity_id = s.product_id', array()) ->where($mainTableAlias.'.store_id = ?', (int)$query->getStoreId()); if ($searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_FULLTEXT || $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE ) { $bind[':query'] = implode(' ', $preparedTerms[0]); $where = Mage::getResourceHelper('catalogsearch') ->chooseFulltext($this->getMainTable(), $mainTableAlias, $select); } if ($likeCond != '' && $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_COMBINE) { $where .= ($where ? ' OR ' : '') . $likeCond; } elseif ($likeCond != '' && $searchType == Mage_CatalogSearch_Model_Fulltext::SEARCH_TYPE_LIKE) { $select->columns(array('relevance' => new Zend_Db_Expr(0))); $where = $likeCond; } if ($where != '') { $select->where($where); } $sql = $adapter->insertFromSelect($select, $this->getTable('catalogsearch/result'), array(), Varien_Db_Adapter_Interface::INSERT_ON_DUPLICATE); $adapter->query($sql, $bind); $query->setIsProcessed(1); } return $this; } }

Sign up to request clarification or add additional context in comments.

Comments

1

Your config.xml should like below.

<global> <models> <catalogsearch> <rewrite> <catalogsearch_fulltext>MyNameSpace_MyModule_Model_CatalogSearch_Fulltext</catalogsearch_fulltext> </rewrite> </catalogsearch> </models> </global> 

And your model class should be like below.

 class MyNameSpace_MyModule_Model_CatalogSearch_Fulltext extends Mage_CatalogSearch_Model_Fulltext { // you should put default declaration of the function here. // inside the function change the development as you want. public function prepareResult($query = null) { die('It works...'); } } 

6 Comments

Hi - thanks. Sorry, that's made no difference at all. Any other ideas?
Also -is there any reason why you're extending Mage_CatalogSearch_Model_Fulltext rather than Mage_CatalogSearch_Model_Resource_Fulltext? Or doesn't it matter?
Also - I'm not following the logic of your config file? shouldn't it be <catalogsearch> for the tag after the <models> tag, then <Mysql4_fulltext> for the tag after <rewrite> ?? There's no folder called catalogsearch under the original catalogsearch folder surely?
@Mat : 1.) First you should clearly identify which class you want to override. 2). In my config.xml <catalogsearch> node is used in the config.xml of the core model declaration. And <catalogsearch_fulltext> node (which is just after the <rewrite> node) should contain the class name after Model of your model class. 3) . If you are pretty much sure about your code just make sure cache is disabled or refreshed.
Thanks for getting back to me Su1 : 1) Okay, the confusion over classes is kind of part of my question, but for clarity's sake - the class is Mage_CatalogSearch_Model_Resource_Fulltext. 2) Sorry, this still isn't clear. Could you re-confirm the config.xml nodes with the class given above please? 3) Have done this with no change
|
0

As pointed out by Ashley, you have to respect the declaration of the method you're overriding.
Someting like that works:

class MyNameSpace_MyModule_Model_CatalogSearch_Fulltext extends Mage_CatalogSearch_Model_Fulltext { public function prepareResult($query = null, $object = null, $queryText = null) { die('It works...'); } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.