2

I just created a module for overriding magento's default search;

But it not working properly. Please help me

See my code below

app/etc/modules/

<?xml version="1.0"?>
<config>
  <modules>
    <Sid_Newsearch>
      <active>true</active>
      <codePool>local</codePool>
      <version>0.1.0</version>
      <depends>
                <Mage_CatalogSearch />

       </depends>
    </Sid_Newsearch>
  </modules>
</config>

Sid/Newsearch/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Sid_Newsearch>
      <version>0.1.0</version>
    </Sid_Newsearch>
  </modules>
  <global>
    <helpers>
      <newsearch>
        <class>Sid_Newsearch_Helper</class>
      </newsearch>
    </helpers>
    <models>
      <newsearch>
        <class>Sid_Newsearch_Model</class>
        <resourceModel>newsearch_mysql4</resourceModel>
      </newsearch>
            <catalogsearch_resource>
                <rewrite>
                    <fulltext>Sid_Newsearch_Model_CatalogSearch_Resource_Fulltext</fulltext>
                </rewrite>
            </catalogsearch_resource>
    </models>
  </global>
</config> 

Sid/Newsearch/Model/CatalogSearch/Resource/Fulltext.php

<?php
class Sid_Newsearch_Model_CatalogSearch_Resource_Fulltext extends Mage_CatalogSearch_Model_Resource_Fulltext
{


    public function prepareResult($object, $queryText, $query)
    {
            echo  '123';
            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) . ')';
                    //echo ;
                }
            }
            $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;
    }
}

?>

Here in the function prepareResult i just added exit() but it is not working;

Please help me , Thanks in advance

1
  • try my latest edited answer. Mar 18, 2015 at 12:50

1 Answer 1

1

As i see you are trying to rewrite the model.so you have to have rewrite the model in the following way

<models>
    <catalogsearch>
        <rewrite>
            <resource_fulltext>Sid_Newsearch_Model_Resource_Fulltext</resource_fulltext>
        </rewrite>
    </catalogsearch>
    <newsearch>
        <class>Sid_Newsearch_Model</class>
        <resourceModel>newsearch_mysql4</resourceModel>
    </newsearch>
</models>

and importantly you are trying to rewrite wrong module which doesn't exists.your class should be

Your class under Sid/Newsearch/Model/Resource/Fulltext.php should be like

class Sid_Newsearch_Model_Resource_Fulltext extends Mage_CatalogSearch_Model_Resource_Fulltext

try avoiding folder called CatalogSearch under Model.

4
  • Hi @Pradeep Sanku : it still not working Mar 19, 2015 at 5:11
  • show me your code. Mar 19, 2015 at 6:18
  • 2
    turn it around <catalogsearch_resource><rewrite><fulltext>Sid_New.... instead of the current
    – Jeroen
    Mar 19, 2015 at 22:45
  • also resourceModel newsearch_mysql4 is deprecated, try only using newsearch_resource and adding them accordingly.
    – Jeroen
    Mar 19, 2015 at 22:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.