0

I want to sort category page products by Popularity. So I created a module.

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>
...

resource collection

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

}

Category model class

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

What is the error with these codes? I can't find. Is there a better way to sort category page products by popl=ularity rather than this?

4

1 に答える 1

1

Your Tal_Popularity_Model_Category class should be more like this:

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