Magento – Sort Products by Date in Admin

datesorting

Often we need to sort products in the admin on creation date. I know I can sort on ID, and this way I'll get the products in the this order, and I found some (free) extensions that give me the option to sort on date on the frontend but that is not what we need.

Is there an easy way to show this option in the admin?

Best Answer

setDefaultSort() of grid class of a module is set grid order field in magento.

As you want the change currenr grid sort order of product then you need go Mage_Adminhtml_Block_Catalog_Product_Grid.Here you see magento already set 'entity_id' as default sort order.

You need change this.

So rewrite class the class Mage_Adminhtml_Block_Catalog_Product_Grid and do whatever you want.

config.xml:

<global>
..... 
    <blocks>
        <adminhtml>
            <rewrite>
            <catalog_product_grid>YourNameSpace_YourModuleName_Block_Adminhtml_Catalog_Product_Grid</catalog_product_grid>
            </rewrite>
        </adminhtml>
....
    </blocks>
  </global>

Class is:

<?php
class YourNameSpace_YourModuleName_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{
 public function __construct()
    {
        parent::__construct();
        $this->setDefaultSort('created_at');
    }
}

Edit:

As per as your coment you want to add created date on product grid then you can the the below code:

class YourNameSpace_YourModuleName_Block_Adminhtml_Catalog_Product_Grid extends Mage_Adminhtml_Block_Catalog_Product_Grid
{

    protected function _prepareCollection(){
        $collection= parent::getCollection();
        $this->setCollection($collection);
        parent::_prepareCollection();
         $this->getCollection()->addAttributeToSelect('created_at');
       return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();

        }
           protected function _prepareColumns()
    {
        parent::_prepareColumns();
        $this->addColumn('created_at',
            array(
                'header'=> Mage::helper('catalog')->__('Created At'),
                'width' => '50px',
                'type'  => 'date',
                'index' => 'created_at',
        ));
        return parent::_prepareColumns();
    }
}
Related Topic