Magento1 catalog seach

magento 提交搜索按钮后执行的代码如下:

<?php
/**
* Catalog Search Controller
*/
class Mage_CatalogSearch_ResultController extends Mage_Core_Controller_Front_Action
{
/**
* Retrieve catalog session
*
* @return Mage_Catalog_Model_Session
*/
protected function _getSession()
{
return Mage::getSingleton('catalog/session');
}
/**
* Display search result
*/
public function indexAction()
{
$query = Mage::helper('catalogsearch')->getQuery();
/* @var $query Mage_CatalogSearch_Model_Query */

$query->setStoreId(Mage::app()->getStore()->getId());

if ($query->getQueryText() != '') {
if (Mage::helper('catalogsearch')->isMinQueryLength()) {
$query->setId(0)
->setIsActive(1)
->setIsProcessed(1);
}
else {
if ($query->getId()) {
$query->setPopularity($query->getPopularity()+1);
}
else {
$query->setPopularity(1);
}

if ($query->getRedirect()){
$query->save();
$this->getResponse()->setRedirect($query->getRedirect());
return;
}
else {
$query->prepare();
}
}

Mage::helper('catalogsearch')->checkNotes();

$this->loadLayout();
$this->_initLayoutMessages('catalog/session');
$this->_initLayoutMessages('checkout/session');
$this->renderLayout();

if (!Mage::helper('catalogsearch')->isMinQueryLength()) {
$query->save();
}
}
else {
$this->_redirectReferer();
}
}
}

1、关键字提交到系统的catalogsearch/result/index控制器,在控制器的$this->loadLayout();前,所有的代码都是判断关键字是否有人用过,用过就热度加1,没用过就把关键字存入数据库,同时还包括关键字的长度个数判断,但是在$this->loadLayout();之前并没有对产品作任何过滤工作,只是单存的把关键字存入数据库而已。

2、$this->loadLayout();在这个过程中,系统加载当前控制器的布局配置文件并初始化相应的Block我的例子中catalogsearch/result/index对应的布局配置如下

<catalogsearch_result_index translate="label">
<label>Quick Search Form</label>
<reference name="root">
<action method="setTemplate"><template>page/2columns-left.phtml</template></action>
</reference>
<reference name="left_first">
<block type="catalogsearch/layer" name="catalogsearch.leftnav" after="currency" template="catalog/layer/view.phtml">
<block type="core/text_list" name="catalog.leftnav.state.renderers" as="state_renderers" />
</block>
</reference>
<reference name="content">
<block type="catalogsearch/result" name="search.result" template="catalogsearch/result.phtml">
<block type="catalog/product_list" name="search_result_list" template="catalog/product/list.phtml">
<block type="core/text_list" name="product_list.name.after" as="name.after" />
<block type="core/text_list" name="product_list.after" as="after" />
<block type="catalog/product_list_toolbar" name="product_list_toolbar" template="catalog/product/list/toolbar.phtml">
<block type="page/html_pager" name="product_list_toolbar_pager"/>
</block>
<action method="addColumnCountLayoutDepend"><layout>empty</layout><count>6</count></action>
<action method="addColumnCountLayoutDepend"><layout>one_column</layout><count>5</count></action>
<action method="addColumnCountLayoutDepend"><layout>two_columns_left</layout><count>4</count></action>
<action method="addColumnCountLayoutDepend"><layout>two_columns_right</layout><count>4</count></action>
<action method="addColumnCountLayoutDepend"><layout>three_columns</layout><count>3</count></action>
<action method="setToolbarBlockName"><name>product_list_toolbar</name></action>
<action method="setColumnCount"><count>4</count></action>
</block>
<action method="setListOrders"/>
<action method="setListModes"/>
<action method="setListCollection"/>
</block>
</reference>
</catalogsearch_result_index>

3、上面配置中的粗体部分是使搜索功能有效的关键所在,解释如下因为有了红色部分的配置,系统会初始化类Mage_CatalogSearch_Block_Layer,初始化的过程如下

 
protected function _construct()
{
 parent::_construct();

 Mage::register('current_layer', $this->getLayer(), true);//这里会注册一个全局变量current_layer,并把类
Mage_CatalogSearch_Model_Layer的实例付给它
 
}
public function getLayer()
{
 return Mage::getSingleton('catalogsearch/layer');
}

4、在catalog/product/list.phtml中会调用对应的block类Mage_Catalog_Block_Product_List中的方法getLoadedProductCollection(),而最终会调用该类的方法getLayer()获取上面定义的全局变量current_layer,并调用current_layer的getProductCollection()获取用关键字过滤过的产品集合作为最终搜索结果

 public function getLayer()
{  
 $layer = Mage::registry('current_layer');

 if ($layer) {

 return $layer;

 }
  
 return Mage::getSingleton('catalog/layer');
 
}

Leave a comment

您的电子邮箱地址不会被公开。 必填项已用 * 标注