2015-05-07 43 views
1

我想在搜索網址中添加結果數。默認的URL就像是domain.com/catalogsearch/result/?q=queries 但我想在URL中添加類似結果計數,magento目錄搜索在網址中添加結果數

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

但問題是它再次觸發。所以它進入無限循環。任何想法來解決這個問題或有什麼辦法來實現這一目標?請幫我..

回答

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); 
+0

它會再次擊中結果頁面,並再次觸發相同的觀察者。所以它進入無盡的循環.. – Elavarasan

0

我不知道這是否是正確的答案,因爲我是新來的Magento爲好。 。 但我在form.mini.phtml添加文本框,並把它下面的「q」文本框這樣

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

而且會導致: domain.com/catalogsearch/result/?q=queries & abc =