我想按人气排序类别页面产品。所以我创建了一个模块。
class Tal_Popularity_Model_Config extends Mage_Catalog_Model_Config
{
public function getAttributeUsedForSortByArray()
{
$options = array(
'position' => Mage::helper('catalog')->__('Position'),
'popularity' => Mage::helper('catalog')->__('Popularity'),
'topsellings' => Mage::helper('catalog')->__('Top Selling')
);
foreach ($this->getAttributesUsedForSortBy() as $attribute)
{
/* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
$options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
}
return $options;
}
}
config.xml中
....
<global>
<models>
<catalog>
<rewrite>
<config>Tal_Popularity_Model_Config</config>
<category>Tal_Popularity_Model_Category</category>
<resourceModel>catalog_resource</resourceModel>
</rewrite>
</catalog>
<catalog_resource>
<rewrite>
<product_collection>Tal_Popularity_Model_Resource</product_collection>
</rewrite>
</catalog_resource>
</models>
<blocks>
<catalog>
<rewrite>
<product_list_toolbar>Tal_Popularity_Block_Product_List_Toolbar</product_list_toolbar>
</rewrite>
</catalog>
</blocks>
...
资源收集
class Tal_Popularity_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_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");
}
}
类别模型类
class Tal_Popularity_Model_Category extends Mage_Catalog_Model_Category
{
public function getProductCollection()
{
// $collection = Mage::getModel('catalog/config')->getCollection()
$collection = Mage::getResourceModel('tal_popularity/product')->getCollection()
->setStoreId($this->getStoreId())
->addCategoryFilter($this);
return $collection;
}
}
这些代码有什么错误?我找不到。有没有更好的方法来通过popl = ularity而不是这个来对类别页面产品进行排序?
答案 0 :(得分:1)
您的Tal_Popularity_Model_Category
课程应该更像这样:
public function getProductCollection()
{
$collection = Mage::getResourceModel('catalog/product_collection')
->setStoreId($this->getStoreId())
->addCategoryFilter($this)
->sortByReview('ASC')
;
return $collection;
}