magento按销量和评论排序

本文介绍如何在Magento中添加按销量和评级排序的功能,通过创建自定义模块并重写相关类,实现产品按销量和评分排序,适用于电商网站的商品展示优化。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  在我们开发的过程中经常会需要添加一些除了系统给出的排序方式之外的其他排序方式,这里Magento开发公司就给大家介绍一下如何添加按销量和评级排序到分类页。

  一、按销量排序

  在这里我们需要建立自己的模块Magease_Catalog.

  1.添加app/etc/modules/Magease_Catalog.xml文件。

  < ?xml version="1.0"? >

  < config >

  < modules >

  < Magease_Catalog >

  < active >true< /active >

  < codePool >community< /codePool >

  < depends >

  < Mage_Catalog / >

  < /depends >

  < /Magease_Catalog >

  < /modules >

  < /config >

  2.创建自己的config.xml,路径为Magease/Catalog/etc/config.xml.

  < ?xml version="1.0"? >

  < config >

  < modules >

  < Magease_Catalog >

  < version >0.1.0< /version >

  < /Magease_Catalog >

  < /modules >

  < global >

  < blocks >

  < catalog >

  < rewrite >

  < product_list_toolbar >Magease_Catalog_Block_Product_List_Toolbar< /product_list_toolbar >

  < /rewrite >

  < /catalog >

  < /blocks >

  < models >

  < catalog >

  < rewrite >

  < config >Magease_Catalog_Model_Config< /config >

  < /rewrite >

  < /catalog >

  < catalog_resource >

  < rewrite >

  < product_collection >Magease_Catalog_Model_Resource_Product_Collection< /product_collection >

  < /rewrite >

  < /catalog_resource >

  < /models >

  < /global >

  < /config >

  3.在这里我们需要重写3个文件。

  Mage_Catalog_Block_Product_List_Toolbar

  Mage_Catalog_Model_Config

  Mage_Catalog_Model_Resource_Product_Collection

  我们的app/code/community/Magease/Catalog/Block/Product/List/Toolbar.php文件内容如下:

  < ?php

  class Magease_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar

  {

  public function setCollection($collection)

  {

  parent::setCollection($collection);

  if ($this- >getCurrentOrder()) {

  if($this- >getCurrentOrder() == 'qty_ordered') {

  $this- >getCollection()- >getSelect()

  - >joinLeft(

  array('order' = > $collection- >getResource()- >getTable('sales/order_item')),

  'e.entity_id = order.product_id',

  array('qty_ordered' = > 'SUM(order.qty_ordered)')

  )

  - >group('e.entity_id')

  - >order('qty_ordered ' . $this- >getCurrentDirection());

  }

  else {

  $this- >getCollection()

  - >setOrder($this- >getCurrentOrder(), $this- >getCurrentDirection())- >getSelect();

  }

  }

  return $this;

  }

  }

  ? >

  我们继承了Mage_Catalog_Block_Product_List_Toolbar中所有的功能和方法但我们自己重写了setCollection()方法

  我们的Magease_Catalog_Model_Config是相当简单的:

  < ?php

  class Magease_Catalog_Model_Config extends Mage_Catalog_Model_Config

  {

  public function getAttributeUsedForSortByArray()

  {

  $options = array(

  'position' = > Mage::helper('catalog')- >__('Position'),

  'qty_ordered' = > Mage::helper('catalog')- >__('Hot Sale')

  );

  foreach ($this- >getAttributesUsedForSortBy() as $attribute) {

  /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */

  $options[$attribute- >getAttributeCode()] = $attribute- >getStoreLabel();

  }

  return $options;

  }

  }

  ? >

  4.到了这一步,产品的排序应该已经奏效,但我们在分页上有点小问题,无法显示正确的数目。我们可以在 Magease/Catalog/Model/Resource/Product/Collection.php中用以下代码来修复这个问题。

  < ?php

  class Magease_Catalog_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection

  {

  protected function _getSelectCountSql($select = null, $resetLeftJoins = true)

  {

  $this- >_renderFilters();

  $countSelect = (is_null($select)) ?

  $this- >_getClearSelect() :

  $this- >_buildClearSelect($select);

  if(count($countSelect- >getPart(Zend_Db_Select::GROUP)) > 0) {

  $countSelect- >reset(Zend_Db_Select::GROUP);

  }

  $countSelect- >columns('COUNT(DISTINCT e.entity_id)');

  if ($resetLeftJoins) {

  $countSelect- >resetJoinLeft();

  }

  return $countSelect;

  }

  }

  ? >

  在4个简单的步骤之后,我们的Magento网站中的商品就能按销量来排序了。

  二、按评分排序

  1.在这里我们需要在config.xml中添加以下代码来重写Mage_Catalog_Block_Product_List:

  < global >

  < blocks >

  < catalog >

  < rewrite >

  < product_list >Magease_Catalog_Block_Product_List< /product_list >

  < /rewrite >

  < /catalog >

  < /blocks >

  < /global >

  2.我们的app/code/community/Magease/Catalog/Block/Product/List.php文件内容如下:

  < ?php

  class Magease_Catalog_Block_Product_List extends Mage_Catalog_Block_Product_List

  {

  protected function _getProductCollection()

  {

  if (is_null($this- >_productCollection)) {

  $layer = $this- >getLayer();

  /* @var $layer Mage_Catalog_Model_Layer */

  if ($this- >getShowRootCategory()) {

  $this- >setCategoryId(Mage::app()- >getStore()- >getRootCategoryId());

  }

  // if this is a product view page

  if (Mage::registry('product')) {

  // get collection of categories this product is associated with

  $categories = Mage::registry('product')- >getCategoryCollection()

  - >setPage(1, 1)

  - >load();

  // if the product is associated with any category

  if ($categories- >count()) {

  // show products from this category

  $this- >setCategoryId(current($categories- >getIterator()));

  }

  }

  $origCategory = null;

  if ($this- >getCategoryId()) {

  $category = Mage::getModel('catalog/category')- >load($this- >getCategoryId());

  if ($category- >getId()) {

  $origCategory = $layer- >getCurrentCategory();

  $layer- >setCurrentCategory($category);

  $this- >addModelTags($category);

  }

  }

  $this- >_productCollection = $layer- >getProductCollection();

  $this- >_productCollection- >joinField('rating_summary', 'review_entity_summary', 'rating_summary', 'entity_pk_value=entity_id', array('entity_type'= >1, 'store_id'= > Mage::app()- >getStore()- >getId()), 'left');

  $this- >prepareSortableFieldsByCategory($layer- >getCurrentCategory());

  if ($origCategory) {

  $layer- >setCurrentCategory($origCategory);

  }

  }

  return $this- >_productCollection;

  }

  }

  ? >

  3.在这里我们需要继续重写Magease_Catalog_Model_Config,在getAttributeUsedForSortByArray()方法中添加以下部分:

  $options = array(

  'rating_summary' = > Mage::helper('catalog')- >__('Rating')

  );

  这时产品的按评分排序就能生效了。

转载于:https://my.oschina.net/u/3840660/blog/3045549

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值