Magento 商品列表按销量和评论排序

2016年3月16日 浏览量 47 4 min read
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')
        );


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

Previous article:
Next article:
Comments
发表评论,留下你的足迹
我们不会公开你的邮箱地址

是否允许我们在发布新内容或者进行促销活动向您发送消息?

Remind me later

Thank you! Please check your email inbox to confirm.

Oops! Notifications are disabled.

© 2014-2023 www.magease.com. All Rights Reserved. 寰云网络 版权所有    鲁ICP备 14014975号-1