Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
Adding New option in catgeory sort by OR Adding Most viewed or most popluar option for sort by in category list page in Magento
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 

Hi,

If you need to added new sort by option like most viewed etc.. follow the steps

override app\code\core\Mage\Catalog\Block\Product\List\Toolbar.php

in local folder and add or override only this 2 functions
Replace default Toolbar.php code by this code (be care full on class name it depends on your module name )

I took ex;- Most Viewed

<?php

class Custommodule_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
   
   
/**
     * Retrieve available Order fields list
     *
     * @return array
     */
        
public function getAvailableOrders()
    
{
    

        $this
->addOrderToAvailableOrders('mostviewd''Most Popular');
        
        
//echo $this->getCurrentOrder(); exit;
        
$this->removeOrderFromAvailableOrders('position');
        
$this->removeOrderFromAvailableOrders('relevance');
        
$this->setDefaultOrder('mostviewd');
        
$this->setDefaultDirection('asc');
        
$this->removeOrderFromAvailableOrders('entity_id');
        
krsort($this->_availableOrder);
        return 
$this->_availableOrder;
    
}
    
    
        
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()) {
            $this
->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        
}
            
if($this->getCurrentOrder()=='mostviewd'{
        $this
->_collection->getSelect()->
                 
joinInner('report_event AS _table_views',
                         
' _table_views.object_id = e.entity_id',
                         
'COUNT(_table_views.event_id) AS views')->
                 
group('e.entity_id')->order('views DESC');
        
}
        
                 $sql 
$this->_collection->getSelectSql(true);
         
           
$this->_collection->getSelect()->reset()->from(
                   array(
'e' =>new Zend_Db_Expr("({$sql})")),
                   array(
'e' => "*")
               );
           
$limit$this->getDefaultPerPageValue();
           
$page =  $this->getRequest()->getParam('p');
           
           if(isset(
$page))
               if((
$page 1) == 0)
                   
$this->_collection->getSelect()->limit($limit);
               else
                   
$this->_collection->getSelect()->limit($limit, ($page 1) * $limit);
           else
               
$this->_collection->getSelect()->limit($limit);
               
           
$this->_collection->load();
           
           
    
//    $this->_collection->printlogquery(true); exit;
        
return $this;
    
}
    
    
    }

clear cache and Enjoy

File Attachments
Toolbar.php  (File Size: 3KB - Downloads: 224)
 
Magento Community Magento Community
Magento Community
Magento Community
 
ljames28
Jr. Member
 
Total Posts:  3
Joined:  2012-01-23
 

Thanks for this, been looking for a better sort order for a while

 
Magento Community Magento Community
Magento Community
Magento Community
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 
pradeep_kumar67 - 21 February 2012 09:56 PM

Hi,

If you need to added new sort by option like most viewed etc.. follow the steps

override app\code\core\Mage\Catalog\Block\Product\List\Toolbar.php

in local folder and add or override only this 2 functions
Replace default Toolbar.php code by this code (be care full on class name it depends on your module name )

I took ex;- Most Viewed

<?php

class Custommodule_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
   
   
/**
     * Retrieve available Order fields list
     *
     * @return array
     */
        
public function getAvailableOrders()
    
{
    

        $this
->addOrderToAvailableOrders('mostviewd''Most Popular');
        
        
//echo $this->getCurrentOrder(); exit;
        
$this->removeOrderFromAvailableOrders('position');
        
$this->removeOrderFromAvailableOrders('relevance');
        
$this->setDefaultOrder('mostviewd');
        
$this->setDefaultDirection('asc');
        
$this->removeOrderFromAvailableOrders('entity_id');
        
krsort($this->_availableOrder);
        return 
$this->_availableOrder;
    
}
    
    
        
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()) {
            $this
->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        
}
            
if($this->getCurrentOrder()=='mostviewd'{
        $this
->_collection->getSelect()->
                 
joinInner('report_event AS _table_views',
                         
' _table_views.object_id = e.entity_id',
                         
'COUNT(_table_views.event_id) AS views')->
                 
group('e.entity_id')->order('views DESC');
        
}
        
                 $sql 
$this->_collection->getSelectSql(true);
         
           
$this->_collection->getSelect()->reset()->from(
                   array(
'e' =>new Zend_Db_Expr("({$sql})")),
                   array(
'e' => "*")
               );
           
$limit$this->getDefaultPerPageValue();
           
$page =  $this->getRequest()->getParam('p');
           
           if(isset(
$page))
               if((
$page 1) == 0)
                   
$this->_collection->getSelect()->limit($limit);
               else
                   
$this->_collection->getSelect()->limit($limit, ($page 1) * $limit);
           else
               
$this->_collection->getSelect()->limit($limit);
               
           
$this->_collection->load();
           
           
    
//    $this->_collection->printlogquery(true); exit;
        
return $this;
    
}
    
    
    }

clear cache and Enjoy

Had a issue when you run in Magebnto1.6.2 so please replace this attached Toolbar.php in block

File Attachments
Toolbar.php  (File Size: 3KB - Downloads: 175)
 
Magento Community Magento Community
Magento Community
Magento Community
 
polyakov
Jr. Member
 
Total Posts:  13
Joined:  2012-03-10
 

Thanks for your code!

Unfortunately on 1.6.2 it has couple bugs:
1. Products that haven’t been viewed yet are not shown.
2. Number of items in category is set to 1
3. Pagination doesn’t show up

I’m looking deeper into your code to make this work, maybe you have some suggestions as well.
Thanks again!

 
Magento Community Magento Community
Magento Community
Magento Community
 
hawks1
Member
 
Total Posts:  49
Joined:  2009-07-06
 

Just tested in 1.6.2 not work too . Thanks any way

 
Magento Community Magento Community
Magento Community
Magento Community
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 

hi

@ hawks1 and @pol

did you fixed this issue ?

 
Magento Community Magento Community
Magento Community
Magento Community
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 

in app\code\core\Mage\Core\Model\Resource\Helper\Mysql4.php

in line 292 you have

“throw new Zend_Db_Exception("Can’t prepare expression when tableName is instance of Zend_Db_Expr");"

comment that line

and also i attached new module which as most view and best seller option in sort option in list page for magneto

once you add and comment above line

File Attachments
 
Magento Community Magento Community
Magento Community
Magento Community
 
Kaushik Patel
Jr. Member
 
Total Posts:  28
Joined:  2011-12-24
 

For sorting the products by created date means new products first.
follow this link.
http://hkpatel201.blogspot.in/2013/03/add-new-sorting-option-in-product.html

 
Magento Community Magento Community
Magento Community
Magento Community
 
ajay1357
Jr. Member
 
Total Posts:  3
Joined:  2011-08-28
 

Doesn’t work for Magento 1.5.1
It says duplicate field price.

 
Magento Community Magento Community
Magento Community
Magento Community
 
mujtabahaider
Jr. Member
 
Total Posts:  2
Joined:  2013-06-16
 

Thanks Pradeep. It’s really helpful. Working on my 1.7.0.2.

 
Magento Community Magento Community
Magento Community
Magento Community
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 

you can use below code which will work fine in magetno1.7

<?php

class Sugarcode_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{

    
public function getAvailableOrders()
    
{
        $this
->addOrderToAvailableOrders('position''Latest Added');
        
$this->addOrderToAvailableOrders('popularity''Popularity');        
        return 
$this->_availableOrder;
    
}
     
public function getTotalNum()
    
{
        Mage
::getModel('core/session')->setTotalNum($this->getCollection()->getSize());
        return 
$this->getCollection()->getSize();
    
}
    
    
    

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);
        
}
        $this
->_collection->getSize();
        if (
$this->getCurrentOrder()) {
            $this
->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        
}
        
        
        
            
if($this->getCurrentOrder()=='popularity'{
                    $this
->_collection->getSelect()->
                 
joinLeft('report_event AS _table_views',
                         
' _table_views.object_id = e.entity_id AND _table_views.store_id='Mage::app()->getStore()->getId(),
                         
'COUNT(_table_views.event_id) AS views')->
                 
group('e.entity_id')->order('views DESC');
        
            
               
            
}
              
        
              
           $limit
$this->getDefaultPerPageValue();
           
$page =  $this->getRequest()->getParam('p');
           
           if(isset(
$page))
               if((
$page 1) == 0)
                   
$this->_collection->getSelect()->limit($limit);
               else
                   
$this->_collection->getSelect()->limit($limit, ($page 1) * $limit);
           else
               
$this->_collection->getSelect()->limit($limit);
                    
           
$this->_collection->load();
           
           
        
$this->_collection->printlogquery(true);
        return 
$this;
    
}

    
    
    
   
/**
     * Get grit products sort order field
     *
     * @return string
     */
    
public function getCurrentOrder()
    
{
        $order 
$this->_getData('_current_grid_order');
        if (
$order{
            
return $order;
        
}

        $orders 
$this->getAvailableOrders();
        
$defaultOrder $this->_orderField;

        if (!isset(
$orders[$defaultOrder])) {
            $keys 
array_keys($orders);
            
$defaultOrder $keys[0];
        
}

        $order 
$this->getRequest()->getParam($this->getOrderVarName());
        if (
$order && isset($orders[$order])) {
            
if ($order == $defaultOrder{
                Mage
::getSingleton('catalog/session')->unsSortOrder();
            
else {
                $this
->_memorizeParam('sort_order'$order);
            
}
        } 
else {
            $order 
Mage::getSingleton('catalog/session')->getSortOrder();
        
}
        
// validate session value
        
if (!$order || !isset($orders[$order])) {
            $order 
$defaultOrder;
        
}
        $this
->setData('_current_grid_order'$order);
        return 
$order;
    
}
    
/**
     * Retrieve current direction
     *
     * @return string
     */
    
public function getCurrentDirection()
    
{
        $dir 
$this->_getData('_current_grid_direction');
        if (
$dir{
            
return $dir;
        
}

        $directions 
= array('asc''desc');
        
$dir strtolower($this->getRequest()->getParam($this->getDirectionVarName()));
        if (
$dir && in_array($dir$directions)) {
            
if ($dir == $this->_direction{
                Mage
::getSingleton('catalog/session')->unsSortDirection();
            
else {
                $this
->_memorizeParam('sort_direction'$dir);
            
}
        } 
else {
            $dir 
Mage::getSingleton('catalog/session')->getSortDirection();
        
}
        
// validate direction
        
if (!$dir || !in_array($dir$directions)) {
            $dir 
$this->_direction;
        
}
        $this
->setData('_current_grid_direction'$dir);
        return 
$dir;
    
}
}

Enjoy grin

 
Magento Community Magento Community
Magento Community
Magento Community
 
pmeems
Jr. Member
 
Total Posts:  19
Joined:  2011-03-27
 

Hi Pradeep,

Thanks for your code snippet. I tried it with v1.7.0.2 but nothing happens.
No extra sorting options are added.

Most likely because I\’m not doing it right wink
I\’m new to the whole overriding Magento stuff.

This is what I did:
1. Created this folder structure: /public_html/app/code/local/Meems/Catalog/Block/Product/List
2. Opened your sample code and changed the class definition to: class Meems_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
3. Saved this file as Toolbar.php
4. Uploaded Toolbar.php to location created in #1
5. Flushed caches and reloaded page.

Could you please be so kind to explain to me what I\’m doing wrong?

Thanks,
Paul

 
Magento Community Magento Community
Magento Community
Magento Community
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 

make sure the block is override correctly
check it giving exit;

pmeems - 19 August 2013 05:04 AM

Hi Pradeep,

Thanks for your code snippet. I tried it with v1.7.0.2 but nothing happens.
No extra sorting options are added.

Most likely because I\’m not doing it right wink
I\’m new to the whole overriding Magento stuff.

This is what I did:
1. Created this folder structure: /public_html/app/code/local/Meems/Catalog/Block/Product/List
2. Opened your sample code and changed the class definition to: class Meems_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
3. Saved this file as Toolbar.php
4. Uploaded Toolbar.php to location created in #1
5. Flushed caches and reloaded page.

Could you please be so kind to explain to me what I\’m doing wrong?

Thanks,
Paul

 
Magento Community Magento Community
Magento Community
Magento Community
 
pradeep_kumar67
Sr. Member
 
Avatar
Total Posts:  160
Joined:  2010-06-24
Bangalore
 

we can implement other sot by options like
Best Seller:- sort my most sold product
Rating:- highest rated product
etc..

<?php
/**
pradeep.kumarrcs67@gmail.com

*/
class Sugarcode_Bestsellersort_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
   
        
public function getAvailableOrders()
    
{
        $this
->addOrderToAvailableOrders('popularity''Popularity');
        
//$this->setDefaultOrder('bestseller');
        
$this->setDefaultDirection('asc');
        return 
$this->_availableOrder;
    
}
        
    
        
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);
        
}
        $this
->_collection->getSize();
        if (
$this->getCurrentOrder()) {
            $this
->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        
}             
        
if($this->getCurrentOrder()=='popularity'{
            $this
->_collection->getSelect()->
            
joinLeft('report_event AS _table_views',
            
' _table_views.object_id = e.entity_id AND _table_views.store_id='Mage::app()->getStore()->getId(),
            
'COUNT(_table_views.event_id) AS views')->
            
group('e.entity_id')->order('views DESC');
        
}

                 
        $limit
$this->getDefaultPerPageValue();
        
$page =  $this->getRequest()->getParam('p');

        if(isset(
$page))
            if((
$page 1) == 0)
                
$this->_collection->getSelect()->limit($limit);
            else
                
$this->_collection->getSelect()->limit($limit, ($page 1) * $limit);
        else
        
$this->_collection->getSelect()->limit($limit);
        
$this->_collection->load();

        
//$this->_collection->printlogquery(true);
        
return $this;
    
}
    

    
/**
    * Get grit products sort order field
    *
    * @return string
    */
    
public function getCurrentOrder()
    
{
        $order 
$this->_getData('_current_grid_order');
        if (
$order{
            
return $order;
        
}
        $orders 
$this->getAvailableOrders();
        
$defaultOrder $this->_orderField;
        if (!isset(
$orders[$defaultOrder])) {
            $keys 
array_keys($orders);
            
$defaultOrder $keys[0];
        
}
        $order 
$this->getRequest()->getParam($this->getOrderVarName());
        if (
$order && isset($orders[$order])) {
            
if ($order == $defaultOrder{
                Mage
::getSingleton('catalog/session')->unsSortOrder();
            
else {
                $this
->_memorizeParam('sort_order'$order);
            
}
        } 
else {
            $order 
Mage::getSingleton('catalog/session')->getSortOrder();
        
}
        
// validate session value
        
if (!$order || !isset($orders[$order])) {
            $order 
$defaultOrder;
        
}
        $this
->setData('_current_grid_order'$order);
        return 
$order;
    
}
    
/**
    * Retrieve current direction
    *
    * @return string
    */
    
public function getCurrentDirection()
    
{
        $dir 
$this->_getData('_current_grid_direction');
        if (
$dir{
            
return $dir;
        
}
        $directions 
= array('asc''desc');
        
$dir strtolower($this->getRequest()->getParam($this->getDirectionVarName()));
        if (
$dir && in_array($dir$directions)) {
            
if ($dir == $this->_direction{
                Mage
::getSingleton('catalog/session')->unsSortDirection();
                
else {
                $this
->_memorizeParam('sort_direction'$dir);
            
}
        } 
else {
            $dir 
Mage::getSingleton('catalog/session')->getSortDirection();
        
}
        
// validate direction
        
if (!$dir || !in_array($dir$directions)) {
            $dir 
$this->_direction;
        
}
        $this
->setData('_current_grid_direction'$dir);
        return 
$dir;
    
}
    
}
File Attachments
Toolbar.php  (File Size: 4KB - Downloads: 5)
 
Magento Community Magento Community
Magento Community
Magento Community
 
witchwatch
Jr. Member
 
Total Posts:  1
Joined:  2013-02-21
 

Hi,

thanks for your code..... 

I ‘m wondering if you have idea of why if i sort by popularity the products are shown in the correct way, but in the layered navigation (using layered navigation seo, catalin seo) the attribute count show the wrong numbers, but if I sort by price or position, the attribute count are correct.

Thanks in advance.

 
Magento Community Magento Community
Magento Community
Magento Community
 
forcize
Jr. Member
 
Total Posts:  1
Joined:  2013-07-29
 
pradeep_kumar67 - 15 September 2013 10:20 PM

we can implement other sot by options like
Best Seller:- sort my most sold product
Rating:- highest rated product
etc..


<?php
/**
pradeep.kumarrcs67@gmail.com

*/
class Sugarcode_Bestsellersort_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
   
        
public function getAvailableOrders()
    
{
        $this
->addOrderToAvailableOrders('popularity''Popularity');
        
//$this->setDefaultOrder('bestseller');
        
$this->setDefaultDirection('asc');
        return 
$this->_availableOrder;
    
}
        
    
        
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);
        
}
        $this
->_collection->getSize();
        if (
$this->getCurrentOrder()) {
            $this
->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        
}             
        
if($this->getCurrentOrder()=='popularity'{
            $this
->_collection->getSelect()->
            
joinLeft('report_event AS _table_views',
            
' _table_views.object_id = e.entity_id AND _table_views.store_id='Mage::app()->getStore()->getId(),
            
'COUNT(_table_views.event_id) AS views')->
            
group('e.entity_id')->order('views DESC');
        
}

                 
        $limit
$this->getDefaultPerPageValue();
        
$page =  $this->getRequest()->getParam('p');

        if(isset(
$page))
            if((
$page 1) == 0)
                
$this->_collection->getSelect()->limit($limit);
            else
                
$this->_collection->getSelect()->limit($limit, ($page 1) * $limit);
        else
        
$this->_collection->getSelect()->limit($limit);
        
$this->_collection->load();

        
//$this->_collection->printlogquery(true);
        
return $this;
    
}
    

    
/**
    * Get grit products sort order field
    *
    * @return string
    */
    
public function getCurrentOrder()
    
{
        $order 
$this->_getData('_current_grid_order');
        if (
$order{
            
return $order;
        
}
        $orders 
$this->getAvailableOrders();
        
$defaultOrder $this->_orderField;
        if (!isset(
$orders[$defaultOrder])) {
            $keys 
array_keys($orders);
            
$defaultOrder $keys[0];
        
}
        $order 
$this->getRequest()->getParam($this->getOrderVarName());
        if (
$order && isset($orders[$order])) {
            
if ($order == $defaultOrder{
                Mage
::getSingleton('catalog/session')->unsSortOrder();
            
else {
                $this
->_memorizeParam('sort_order'$order);
            
}
        } 
else {
            $order 
Mage::getSingleton('catalog/session')->getSortOrder();
        
}
        
// validate session value
        
if (!$order || !isset($orders[$order])) {
            $order 
$defaultOrder;
        
}
        $this
->setData('_current_grid_order'$order);
        return 
$order;
    
}
    
/**
    * Retrieve current direction
    *
    * @return string
    */
    
public function getCurrentDirection()
    
{
        $dir 
$this->_getData('_current_grid_direction');
        if (
$dir{
            
return $dir;
        
}
        $directions 
= array('asc''desc');
        
$dir strtolower($this->getRequest()->getParam($this->getDirectionVarName()));
        if (
$dir && in_array($dir$directions)) {
            
if ($dir == $this->_direction{
                Mage
::getSingleton('catalog/session')->unsSortDirection();
                
else {
                $this
->_memorizeParam('sort_direction'$dir);
            
}
        } 
else {
            $dir 
Mage::getSingleton('catalog/session')->getSortDirection();
        
}
        
// validate direction
        
if (!$dir || !in_array($dir$directions)) {
            $dir 
$this->_direction;
        
}
        $this
->setData('_current_grid_direction'$dir);
        return 
$dir;
    
}
    
}

Ty for the code!

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2