2

我正在尝试覆盖 Magento 的目录搜索功能,以便它使用“AND”而不是“OR”来搜索搜索词。

这样做的“正确”方法,即更新证明,是创建我自己的模块。所以这就是我所做的。不幸的是,这不起作用。未调用更新的方法。

这是配置文件部分:

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

我试图覆盖的方法是 Mage_CatalogSearch_Model_Fulltext,我实际上正在做一个“扩展” - 例如,

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

我在某处读过,如果您实际尝试覆盖的类从未使用特定的类“创建者”方法调用,那么执行此“覆盖”是没有意义的?如果这是真的,这可以解释为什么我的新类方法从未使用过?

那么在那种情况下,如何重写这个方法呢?我在这里做正确的事吗?

4

3 回答 3

3

我已经成功覆盖了相同的功能查看代码:etc->modules:LevoSoft_CatalogSearch.xml

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

配置文件

<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;
    }
}

于 2015-06-02T11:14:11.707 回答
1

您的 config.xml 应该如下所示。

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

你的模型类应该如下所示。

  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...');
        }
    }
于 2013-09-30T09:06:56.593 回答
0

正如 Ashley 所指出的,您必须尊重您要覆盖的方法的声明。
像这样的工作:

class MyNameSpace_MyModule_Model_CatalogSearch_Fulltext extends Mage_CatalogSearch_Model_Fulltext
{

    public function prepareResult($query = null, $object = null, $queryText = null)
    {
        die('It works...');
    }
}
于 2013-09-29T12:30:40.040 回答