Skip to main content

Sort Products by popularity count in Magento

Advance shorting option for category/list page in magento
Note: Please Make  folder structure app/code/local  and place all files in same structure in this folder ,If you make changes in core files its your risk.please make proper backup for core files if you modify that .


Step -1.
For Advance shorting option you have to create new collection by overriding Collection class.Overrride the collection class(/app/code/core/Mage/Catalog/Model/Resource/Product/collection.php and create a new function
<?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"); 
 }

 ?>


Step-2 Find the file config.php(/app/code/core/Mage/Catalog/Model/config.php) and search for    getAttributeUsedForSortByArray(), And add here search option name  you get an array which you want to show in  toolbar drop-down in product-listing  page in your magento store.(line 4)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
<?php public function getAttributeUsedForSortByArray()
   {
       $options = array(
              'popularity'  => Mage::helper('catalog')->__('Popularty'),
             '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;
   }
  ?>

Step:3 Finally call overridden collection(created onStep-1) when Popularity attribute  (created on step 2) selected for sorting in list page. for this find 

/app/code/core/Mage/Catalog/Block/Product/List /toolbar.php
and include these lines.(line no..11 to 17) in setCollection method.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
<?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;
   }?>
Now you can see popularity option working in product listing page.

Comments

Popular posts from this blog

get directory paths, get Base URLs, get URLs in static block, get secure URL in Magento

1) Get Directory paths  Mage::getBaseDir()  //output : /var/www/html/magento Mage::getBaseDir('app')  //output : /var/www/html/magento/app Mage::getBaseDir('media') //output : /var/www/html/magento/media Mage::getBaseDir(‘design’) => Get design directory path Mage::getBaseDir(‘code’) => Gives code directory file path Mage::getBaseDir(‘lib’) => Gives lib directory file path Mage::getBaseDir(‘skin’) => Gives skin directory file path Mage::getBaseDir(‘var’) => Gives var directory file path Mage::getBaseDir(‘cache’) => Gives cache directory file path Mage::getBaseDir(‘log’) => Gives log directory file path       2) Get Base URL  Mage::getBaseUrl() => Get base url path e.g. http://yourwebsite.com/  Mage::getBaseUrl('media') => Get MEDIA folder path e.g. http://yourwebsite.com/media/  Mage::getBaseUrl('js') => Get JS folder path e.g. http://yourwebsite.com/js/  Mage::getBaseUrl('skin') => Get SKIN folder p

CodeIgniter Disallowed Key Characters

Open libraries/Input.php (system/core/Input.php in CI version 2.0+) and locate function _clean_input_keys($str){, The whole block should look like so: function _clean_input_keys($str) {     if ( ! preg_match("/^[a-z0-9:_\/-]+$/i", $str))     {         exit('Disallowed Key Characters.');     }     return $str; } Modify the PCRE sot that it allows the new chars. Please not that the char thats missing is the .(dot) and you should always escape the .(dot) in Regular Expressions as they will otherwise allow any single char. /^[a-z0-9:_\/-\.]+$/i