Magento产品列表排序问题
问题描述:
如果我想按名称升序对结果列表进行排序,Magento会记住此首选项,并按名称升序对所有未来的“搜索”和“根类别”进行排序,即使这不适用于搜索...你总是希望Relevance是默认的。Magento产品列表排序问题
这是如何改变的,所以Magento忘记排序偏好?
答
Magento在目录会话中存储目录排序数据(页码除外)。当应用类别排序时,会话数据和URL GET
参数都会使用,如果它们存在(然后更新记录的会话数据),则首选GET
参数。排序数据从会话如下拉:
Mage::getSingleton('catalog/session')->getSortOrder();
Mage::getSingleton('catalog/session')->getSortDirection();
Mage::getSingleton('catalog/session')->getDisplayMode();
Mage::getSingleton('catalog/session')->getLimitPage();
你也可以取消设置使用下列命令,这个会话数据:
Mage::getSingleton('catalog/session')->unsSortOrder();
Mage::getSingleton('catalog/session')->unsSortDirection();
Mage::getSingleton('catalog/session')->unsDisplayMode();
Mage::getSingleton('catalog/session')->unsLimitPage();
如果搜索这些命令的代码库它迅速变成了工具栏类Mage_Catalog_Block_Product_List_Toolbar
其中包含的方法:
getCurrentOrder()
getCurrentDirection()
getCurrentMode()
getLimit()
每个方法拉动相关排序数据首先通过查看请求参数(因此GET
参数),如果失败则查看会话,如果没有会话数据,则最终回退到默认排序设置。还要注意的是,如果找到GET
参数,那么对$this->_memorizeParam(...);
的调用不是该参数的默认排序。
为了尽量减少对核心功能的影响,我建议你最好的方法应该是沿着重写上述方法的方向,并且在新方法中调用上述相关会话方法以取消该参数的会话数据设置,并通过parent::
调用父方法完成。这样会话数据永远不会被找到,只有URL参数或默认排序会被使用。一个例子重写会是这样的模块中3210文件:
<?xml version="1.0"?>
<config>
<modules>
<Namespace_ModuleName>
<version>0.1.0</version>
</Namespace_ModuleName>
</modules>
....
<global>
...
<blocks>
<catalog>
<rewrite>
<product_list_toolbar>Namespace_ModuleName_Block_Product_List_Toolbar</product_list_toolbar>
</rewrite>
</catalog>
</blocks>
</global>
</config>
而且你重写类:
<?php
class Namespace_ModuleName_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
public function getCurrentOrder()
{
Mage::getSingleton('catalog/session')->unsSortOrder();
parent::getCurrentOrder();
}
public function getCurrentDirection()
{
Mage::getSingleton('catalog/session')->unsSortDirection();
parent::getCurrentDirection();
}
public function getCurrentMode()
{
Mage::getSingleton('catalog/session')->unsDisplayMode();
parent::getCurrentMode();
}
public function getLimit()
{
Mage::getSingleton('catalog/session')->unsLimitPage();
parent::getLimit();
}
}