目录搜索:如果只有一个结果,则显示插入列表视图的产品视图页面


14

我的目标是在Magento目录搜索中实现以下修改。

当我搜索产品时,结果集中仅返回一个产品,我想显示到产品视图页面而不是产品列表页面。

您能否给我建议,我应该如何以及在哪里开始进行修改?

此刻,我真的迷失了很多代码。

使用Magento 1.9.0.1

Answers:


21

您需要创建一个新扩展名,以在呈现快速搜索(或高级搜索)页面之前检查产品集合中是否仅包含一个产品。
为此,我们创建一个新的扩展名为StackExchange_CatalogSearch
您将需要以下文件:

app/etc/modules/StackExchange_CatalogSearch.xml -声明文件

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

app/code/local/StackExchange/CatalogSearch/etc/config.xml -配置文件:

<?xml version="1.0"?>
<config>
    <modules>
        <StackExchange_CatalogSearch>
            <version>1.0.0</version>
        </StackExchange_CatalogSearch>
    </modules>
    <global>
        <models>
            <stackexchange_catalogsearch>
                <class>StackExchange_CatalogSearch_Model</class>
            </stackexchange_catalogsearch>
        </models>
    </global>
    <frontend>
        <events>
            <controller_action_layout_render_before_catalogsearch_result_index><!-- for the quick search-->
                <observers>
                    <stackexchange_catalogsearch>
                        <model>stackexchange_catalogsearch/observer</model>
                        <method>redirectToProduct</method>
                    </stackexchange_catalogsearch>
                </observers>
            </controller_action_layout_render_before_catalogsearch_result_index>
            <controller_action_layout_render_before_catalogsearch_advanced_result><!-- for the advanced search-->
                <observers>
                    <stackexchange_catalogsearch>
                        <model>stackexchange_catalogsearch/observer</model>
                        <method>redirectToProduct</method>
                    </stackexchange_catalogsearch>
                </observers>
            </controller_action_layout_render_before_catalogsearch_advanced_result>
        </events>
    </frontend>
</config>

app/code/local/StackExchange/CatalogSearch/Model/Observer.php -完成所有工作的观察员。

<?php
class StackExchange_CatalogSearch_Model_Observer
{
    //the product list block name in layout
    const RESULT_BLOCK_NAME = 'search_result_list';
    public function redirectToProduct($observer)
    {
        /** @var Mage_Catalog_Block_Product_List $block */
        $block = Mage::app()->getLayout()->getBlock(self::RESULT_BLOCK_NAME);
        if ($block) {
            $collection = $block->getLoadedProductCollection();
            if ($collection && $collection->getSize() == 1) {
                /** @var Mage_Catalog_Model_Product $product */
                $product = $collection->getFirstItem();
                $url = $product->getProductUrl();
                if ($url){
                    Mage::app()->getResponse()->setRedirect($url);
                    Mage::app()->getResponse()->sendResponse();
                    exit; //stop everything else
                }
            }
        }
    }
}

清除缓存,禁用编译(如果启用),然后尝试。

注意:当搜索(和高级搜索)页面仅应在产品上返回时,即使在搜索后或应用分层导航过滤器后发生,此扩展程序也会重定向到产品页面。


哇,太棒了!非常感谢你!
Marco

1
对于那些懒惰的人,请在这里下载:github.com/sreichel/magento-StackExchange_CatalogSearch
sv3n
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.