61. Catalog 分类页面商品排序

这里写图片描述

在后台修改:
这里写图片描述

如果想增加排序的属性:
这里写图片描述
这里写图片描述


在分类页面添加自定义商品排序 Custom Product Sorting In Category Page

如果我们想要添加自定义的商品排序, 例如: 以上架日期或更新日期排序, 为了达到该效果, 
我们就需要自己进行编写了

这里我们先来拿上架日期来做简单的举例, 首先要在我们的下拉框里添加这个选项
找到 Mage_Catalog_Model_Config 文件, 对其 getAttributeUsedForSortByArray() 进行修改
public function getAttributeUsedForSortByArray()
{
   $options = array(
       'position'    => Mage::helper('catalog')->__('Position'),
       'created_at'  => Mage::helper('catalog')->__('Product Creation Date')
   );

   foreach ($this->getAttributesUsedForSortBy() as $attribute) {
       /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
       $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
   }

   return $options;
}
你最好重写这个类来对其进行修改

接下来对我们的集合(Collection)进行修改, 这样就可以根据我们自定义下拉框的内容进行排序了,
 不过 “created_at” 已经是一个产品自带的属性, 所以对于这个我们不需要做任何额外的操作

如果我们想通过评论来进行排序 Sort Products By Number Of Reviews

因为评论不是一个产品自带的属性, 所以这次我们就需要对产品集合(Product Collection)进行修改,
 这里就不重提如何在排序下拉框中添加选项了, 和前面的操作一样

打开文件 Mage_Catalog_Block_Product_List_Toolbar, 在大概 221 行的位置找到 setCollection() 方法, 
同样的你最好在你自己的模块重写这个类, 然后复写 setCollection() 方法, 如下:
public function setCollection($collection)
{
    $this->_collection = $collection;

    $this->_collection->setCurPage($this->getCurrentPage());

    // we need to set pagination only if passed value integer and more that 0
    $limit = (int)$this->getLimit();
    if ($limit) {
        $this->_collection->setPageSize($limit);
    }

    if($this->getCurrentOrder() == 'review'){
        $this->_collection->sortByReview($this->getCurrentDirection());
    } else if ($this->getCurrentOrder()) {
        $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
    }

    return $this;
}
从这里你可以看出我们在 Collection 里添加了一个新的方法 sortByReview(), 
所以我们要重写产品集合类 Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection 
来添加该方法
public function sortByReview($dir)
{
    $table = $this->getTable('review/review');

    $entity_code_id = Mage::getModel('review/review')
                            ->getEntityIdByCode(
                                Mage_Rating_Model_Rating::ENTITY_PRODUCT_CODE
                            );

    $cond = $this->getConnection()->quoteInto('t2.entity_pk_value = e.entity_id and ', '')
          . $this->getConnection()->quoteInto('t2.entity_id = ? ', $entity_code_id);

    $this->getSelect()
         ->joinLeft(
                array('t2'=>$table), 
                $cond,
                array('review' => new Zend_Db_Expr('count(review_id)'))
         )
         ->group('e.entity_id')
         ->order("review $dir");
}

http://www.sunzhenghua.com/magento-category-page-product-sorting

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值