magento目录搜索在url中添加结果计数

时间:2015-05-07 05:14:36

标签: php magento observer-pattern

我想在搜索网址中添加结果计数。默认网址类似于domain.com/catalogsearch/result/?q=queries 但是我想在url中添加结果计数,

`domain.com/catalogsearch/result/?q=queries&count=50`

我尝试过下面的事件观察员

controller_action_layout_render_before_catalogsearch_result_index

Observer.php

 <?php
    class PackgeName_ModuleName_Model_Observer {
        // the product list block name in layout
        const RESULT_BLOCK_NAME = 'search_result_list';
        public function loadResult(Varien_Event_Observer $observer) {
            $currentUrl = Mage::helper ( 'core/url' )->getCurrentUrl ();

            $block = Mage::app ()->getLayout ()->getBlock ( self::RESULT_BLOCK_NAME );
            if ($block) {

                $collection = $block->getLoadedProductCollection ();

                /**
                 * @var Mage_Catalog_Model_Product $product
                 */
                $count = $collection->getSize ();

                $resultUrl = $currentUrl . '&count=' . $count;

                if ($resultUrl) {

                    Mage::app ()->getResponse ()->setRedirect ( $resultUrl );
                    exit (); // stop everything else
                }
            }
        }
    }

但问题是它又被触发了。所以它进入无限循环。任何想法来解决这个问题或者有什么方法可以解决这个问题吗?请帮帮我..

2 个答案:

答案 0 :(得分:0)

只需添加一项检查,表明计数不在请求uri中:

    public function loadResult(Varien_Event_Observer $observer) {

       $count = Mage::app()->getRequest()->getParam('count');

       if (!$count){

           $currentUrl = Mage::helper ( 'core/url' )->getCurrentUrl ();

           $block = Mage::app ()->getLayout ()->getBlock ( self::RESULT_BLOCK_NAME );

答案 1 :(得分:0)

我不知道这是否是正确的答案,因为我也是Magento的新手。 但我在form.mini.phtml中添加了textfield并将其放在“q”文本字段下面

<input type="text" id="abc" name="abc" value="" >

它将导致: domain.com/catalogsearch/result/?q=queries&abc =