3

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?

1 Answer 1

3

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();
    }
}
3
  • Hi, and do I see the date of creation in the grid? Or are the products only sorted at this date?
    – Lucky
    Jun 30, 2015 at 13:59
  • Product sort in admin
    – Amit Bera
    Jun 30, 2015 at 14:00
  • Maybe my question was not complete, we would like to see the date of creation in the product grid, and then sort on this value.
    – Lucky
    Jun 30, 2015 at 14:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.