magento|列表页默认position排序的时候产品显示动态变化
如题,默认使用position排序,版本是1.9,原版本默认使用分类中设置的position的值正序,越小的越靠前,这个也可以通过代码调整。而且,position值可以使用负数。
修改排序默认正序或者倒序的位置是:
app\code\local\Mage\Catalog\Block\Product\List\Toolbar.php中第119行,
protected $_direction = 'asc';
更改asc为desc即可变更默认的正序为倒序。
但是,在使用过程中却发现一个问题,默认position为1,如果不更改的话,本地测试没问题,线上却出现了每一次刷新的列表产品的顺序动态变化,简直了。输出sql查询语句发现,使用的ORDER BY字段只有一个position。那么为了解决position值相同的情况下产品顺序不固定的问题,需要再加一个排序字段,使用双字段排序,这样就能够在position相同的时候,其他值不同。
具体解决访问如下:
同样是前面的Toolbar.php文件,其中的setCollection方法,第232行,原代码:
if ($this->getCurrentOrder()) { $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection()); }
修改为:
if(($this->getCurrentOrder())=='position'){ $this->_collection->addAttributeToSort('position', $this->getCurrentDirection())->addAttributeToSort('created_at', $this->getCurrentDirection()); }
当然,这只是限定默认情况下的排序,如果有其他特殊的排序需求,也可以在同一位置进行修改。