0

我想按受欢迎程度对类别页面产品进行排序。所以我创建了一个模块。

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;
    }
}

配置文件

....
<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 而不是这样对类别页面产品进行排序?

4

1 回答 1

1

你的Tal_Popularity_Model_Category班级应该更像这样:

public function getProductCollection()
{
    $collection = Mage::getResourceModel('catalog/product_collection')
        ->setStoreId($this->getStoreId())
        ->addCategoryFilter($this)
        ->sortByReview('ASC')
    ;
    return $collection;
}
于 2013-03-24T08:18:58.193 回答