Monday 1 September 2014

product list sort by popularity in magento

follow the following step :-

first you go to  following directory:-

/app/code/core/Mage/Catalog/Model/Resource/Product/collection.php

isert this code
<?php
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");

 }
?>
second open below directory:---
/app/code/core/Mage/Catalog/Model/Config.php
find "getAttributeUsedForSortByArray" function and replace it these code

<?php
public function getAttributeUsedForSortByArray()
    {
        $options = array(
            'position'  => Mage::helper('catalog')->__('Position'),
            'popularity'  => Mage::helper('catalog')->__('Popularty')
            
        );
        foreach ($this->getAttributesUsedForSortBy() as $attribute) {
            /* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
            $options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
        }

        return $options;
    }
?>
Last open below directory:---
/app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php

find "setCollection" function and replace it these code
<?php

public function setCollection($collection)
   {
       $this->_collection = $collection;
       $this->_collection->setCurPage($this->getCurrentPage());
       // we need to set pagination only if passed value integer and more that 0
       $limit = (int)$this->getLimit();
       if ($limit) {
           $this->_collection->setPageSize($limit);
       }
        if($this->getCurrentOrder() == 'popularity'){
           $this->_collection->sortByReview($this->getCurrentDirection());
       }
     else if ($this->getCurrentOrder()) {
      
           $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
    }
  
       return $this;
   }
?>

No comments:

Post a Comment