1

I just installed Magento 1.9.3.0 CE with sample data in my Windows 7 system.

I have customized Product Admin Grid area by overriding the file magento193\app\code\core\Mage\Adminhtml\Block\Catalog\Product\Grid.php into local codePool and the overrided file is below:

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://opensource.org/licenses/osl-3.0.php
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@magento.com so we can send you a copy immediately.
 *
 * DISCLAIMER
 *
 * Do not edit or add to this file if you wish to upgrade Magento to newer
 * versions in the future. If you wish to customize Magento for your
 * needs please refer to http://www.magento.com for more information.
 *
 * @category    Mage
 * @package     Mage_Adminhtml
 * @copyright  Copyright (c) 2006-2016 X.commerce, Inc. and affiliates (http://www.magento.com)
 * @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
 */

/**
 * Adminhtml customer grid block
 *
 * @category   Mage
 * @package    Mage_Adminhtml
 * @author      Magento Core Team <core@magentocommerce.com>
 */
class Mage_Adminhtml_Block_Catalog_Product_Grid extends Mage_Adminhtml_Block_Widget_Grid
{

    public function __construct()
    {
        parent::__construct();
        $this->setId('productGrid');
        $this->setDefaultSort('entity_id');
        $this->setDefaultDir('DESC');
        $this->setSaveParametersInSession(true);
        $this->setUseAjax(true);
        $this->setVarNameFilter('product_filter');

    }

    protected function _getStore()
    {
        $storeId = (int) $this->getRequest()->getParam('store', 0);
        return Mage::app()->getStore($storeId);
    }

    protected function _prepareCollection()
    {
        $store = $this->_getStore();
        $collection = Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToSelect('sku')
            ->addAttributeToSelect('name')
            ->addAttributeToSelect('attribute_set_id')
            ->addAttributeToSelect('type_id');

        if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
            $collection->joinField('qty',
                'cataloginventory/stock_item',
                'qty',
                'product_id=entity_id',
                '{{table}}.stock_id=1',
                'left');
        }
        if ($store->getId()) {
            //$collection->setStoreId($store->getId());
            $adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
            $collection->addStoreFilter($store);
            $collection->joinAttribute(
                'name',
                'catalog_product/name',
                'entity_id',
                null,
                'inner',
                $adminStore
            );
            $collection->joinAttribute(
                'custom_name',
                'catalog_product/name',
                'entity_id',
                null,
                'inner',
                $store->getId()
            );
            $collection->joinAttribute(
                'status',
                'catalog_product/status',
                'entity_id',
                null,
                'inner',
                $store->getId()
            );
            $collection->joinAttribute(
                'visibility',
                'catalog_product/visibility',
                'entity_id',
                null,
                'inner',
                $store->getId()
            );
            $collection->joinAttribute(
                'price',
                'catalog_product/price',
                'entity_id',
                null,
                'left',
                $store->getId()
            );
        }
        else {
            $collection->addAttributeToSelect('price');
            $collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
            $collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
        }

        $collection->joinTable(
            array('sfoi' => 'sales/order_item'), 'product_id = entity_id',
            array('last_sold_date' => 'sfoi.created_at')
        );

        $collection->getSelect()->order('sfoi.created_at','DESC')->limit(1);
        //echo $collection->getSelect();

        $this->setCollection($collection);

        parent::_prepareCollection();
        $this->getCollection()->addWebsiteNamesToResult();
        return $this;
    }

    protected function _addColumnFilterToCollection($column)
    {
        if ($this->getCollection()) {
            if ($column->getId() == 'websites') {
                $this->getCollection()->joinField('websites',
                    'catalog/product_website',
                    'website_id',
                    'product_id=entity_id',
                    null,
                    'left');
            }
        }
        return parent::_addColumnFilterToCollection($column);
    }

    protected function _prepareColumns()
    {
        $this->addColumn('entity_id',
            array(
                'header'=> Mage::helper('catalog')->__('ID'),
                'width' => '50px',
                'type'  => 'number',
                'index' => 'entity_id',
        ));

        $this->addColumn('name',
            array(
                'header'=> Mage::helper('catalog')->__('Name'),
                'index' => 'name',
        ));

        $store = $this->_getStore();
        if ($store->getId()) {
            $this->addColumn('custom_name',
                array(
                    'header'=> Mage::helper('catalog')->__('Name in %s', $store->getName()),
                    'index' => 'custom_name',
            ));
        }

        $this->addColumn('type',
            array(
                'header'=> Mage::helper('catalog')->__('Type'),
                'width' => '60px',
                'index' => 'type_id',
                'type'  => 'options',
                'options' => Mage::getSingleton('catalog/product_type')->getOptionArray(),
        ));

        $sets = Mage::getResourceModel('eav/entity_attribute_set_collection')
            ->setEntityTypeFilter(Mage::getModel('catalog/product')->getResource()->getTypeId())
            ->load()
            ->toOptionHash();

        $this->addColumn('set_name',
            array(
                'header'=> Mage::helper('catalog')->__('Attrib. Set Name'),
                'width' => '100px',
                'index' => 'attribute_set_id',
                'type'  => 'options',
                'options' => $sets,
        ));

        $this->addColumn('sku',
            array(
                'header'=> Mage::helper('catalog')->__('SKU'),
                'width' => '80px',
                'index' => 'sku',
        ));

        $store = $this->_getStore();
        $this->addColumn('price',
            array(
                'header'=> Mage::helper('catalog')->__('Price'),
                'type'  => 'price',
                'currency_code' => $store->getBaseCurrency()->getCode(),
                'index' => 'price',
        ));

        if (Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')) {
            $this->addColumn('qty',
                array(
                    'header'=> Mage::helper('catalog')->__('Qty'),
                    'width' => '100px',
                    'type'  => 'number',
                    'index' => 'qty',
            ));
        }

        $this->addColumn('last_sold_date', array(
            'header' => Mage::helper('catalog')->__('Last Sold Date'),
            'width' => '100px',
            'type' => 'datetime',
            'index' => 'last_sold_date',
            //'filter_index' => 'sfoi.created_at',
            //'frame_callback' => array($this, 'styleDate')
            'filter_condition_callback' => array($this, '_filterByDate')
        ));

        $this->addColumn('visibility',
            array(
                'header'=> Mage::helper('catalog')->__('Visibility'),
                'width' => '70px',
                'index' => 'visibility',
                'type'  => 'options',
                'options' => Mage::getModel('catalog/product_visibility')->getOptionArray(),
        ));

        $this->addColumn('status',
            array(
                'header'=> Mage::helper('catalog')->__('Status'),
                'width' => '70px',
                'index' => 'status',
                'type'  => 'options',
                'options' => Mage::getSingleton('catalog/product_status')->getOptionArray(),
        ));

        if (!Mage::app()->isSingleStoreMode()) {
            $this->addColumn('websites',
                array(
                    'header'=> Mage::helper('catalog')->__('Websites'),
                    'width' => '100px',
                    'sortable'  => false,
                    'index'     => 'websites',
                    'type'      => 'options',
                    'options'   => Mage::getModel('core/website')->getCollection()->toOptionHash(),
            ));
        }

        $this->addColumn('action',
            array(
                'header'    => Mage::helper('catalog')->__('Action'),
                'width'     => '50px',
                'type'      => 'action',
                'getter'     => 'getId',
                'actions'   => array(
                    array(
                        'caption' => Mage::helper('catalog')->__('Edit'),
                        'url'     => array(
                            'base'=>'*/*/edit',
                            'params'=>array('store'=>$this->getRequest()->getParam('store'))
                        ),
                        'field'   => 'id'
                    )
                ),
                'filter'    => false,
                'sortable'  => false,
                'index'     => 'stores',
        ));

        if (Mage::helper('catalog')->isModuleEnabled('Mage_Rss')) {
            $this->addRssList('rss/catalog/notifystock', Mage::helper('catalog')->__('Notify Low Stock RSS'));
        }

        return parent::_prepareColumns();
    }

    protected function _prepareMassaction()
    {
        $this->setMassactionIdField('entity_id');
        $this->getMassactionBlock()->setFormFieldName('product');

        $this->getMassactionBlock()->addItem('delete', array(
             'label'=> Mage::helper('catalog')->__('Delete'),
             'url'  => $this->getUrl('*/*/massDelete'),
             'confirm' => Mage::helper('catalog')->__('Are you sure?')
        ));

        $statuses = Mage::getSingleton('catalog/product_status')->getOptionArray();

        array_unshift($statuses, array('label'=>'', 'value'=>''));
        $this->getMassactionBlock()->addItem('status', array(
             'label'=> Mage::helper('catalog')->__('Change status'),
             'url'  => $this->getUrl('*/*/massStatus', array('_current'=>true)),
             'additional' => array(
                    'visibility' => array(
                         'name' => 'status',
                         'type' => 'select',
                         'class' => 'required-entry',
                         'label' => Mage::helper('catalog')->__('Status'),
                         'values' => $statuses
                     )
             )
        ));

        if (Mage::getSingleton('admin/session')->isAllowed('catalog/update_attributes')){
            $this->getMassactionBlock()->addItem('attributes', array(
                'label' => Mage::helper('catalog')->__('Update Attributes'),
                'url'   => $this->getUrl('*/catalog_product_action_attribute/edit', array('_current'=>true))
            ));
        }

        Mage::dispatchEvent('adminhtml_catalog_product_grid_prepare_massaction', array('block' => $this));
        return $this;
    }

    public function getGridUrl()
    {
        return $this->getUrl('*/*/grid', array('_current'=>true));
    }

    public function getRowUrl($row)
    {
        return $this->getUrl('*/*/edit', array(
            'store'=>$this->getRequest()->getParam('store'),
            'id'=>$row->getId())
        );
    }

    /*public function styleDate( $value,$row,$column,$isExport ) {
        $locale = Mage::app()->getLocale();
        $date = $locale->date($value,
                            $locale->getDateFormat(),
                            $locale->getLocaleCode(),
                            false
                        )
                        ->toString( $locale->getDateFormat());
        return $date;
    }*/

    protected function _filterByDate($value, $index = null) {
        $collection = $this->getCollection();
        //How to divide $value into $fromDate & $toDate
        $collection->addFieldToFilter('sfoi.created_at', array('gteq' => $fromDate));
        $collection->addFieldToFilter('sfoi.created_at', array('lteq' => $toDate));
    }
}

Now I have managed somehow to get the "Last Sold Date" column for each product in the grid, by applying a join and sorting in the Product Collection query.

But the error arises when I try to filter the grid by applying/providing "From date" and "To date" values into "Last Sold Date" column top filter. The error is as below:

a:5:{i:0;s:39:"Invalid attribute name: sfoi.created_at";i:1;s:6212:"#0 C:\UwAmp\www\magento193\app\code\core\Mage\Eav\Model\Entity\Collection\Abstract.php(1291): Mage::exception('Mage_Eav', 'Invalid attribu...')
#1 C:\UwAmp\www\magento193\app\code\core\Mage\Eav\Model\Entity\Collection\Abstract.php(1384): Mage_Eav_Model_Entity_Collection_Abstract->_addAttributeJoin('sfoi.created_at', 'inner')
#2 C:\UwAmp\www\magento193\app\code\core\Mage\Eav\Model\Entity\Collection\Abstract.php(321): Mage_Eav_Model_Entity_Collection_Abstract->_getAttributeConditionSql('sfoi.created_at', Array, 'inner')
#3 C:\UwAmp\www\magento193\app\code\core\Mage\Catalog\Model\Resource\Product\Collection.php(1440): Mage_Eav_Model_Entity_Collection_Abstract->addAttributeToFilter('sfoi.created_at', Array, 'inner')
#4 C:\UwAmp\www\magento193\app\code\core\Mage\Eav\Model\Entity\Collection\Abstract.php(341): Mage_Catalog_Model_Resource_Product_Collection->addAttributeToFilter('sfoi.created_at', Array)
#5 C:\UwAmp\www\magento193\app\code\local\Mage\Adminhtml\Block\Catalog\Product\Grid.php(361): Mage_Eav_Model_Entity_Collection_Abstract->addFieldToFilter('sfoi.created_at', Array)
#6 [internal function]: Mage_Adminhtml_Block_Catalog_Product_Grid->_filterByDate(Object(Mage_Catalog_Model_Resource_Product_Collection), Object(Mage_Adminhtml_Block_Widget_Grid_Column))
#7 C:\UwAmp\www\magento193\app\code\core\Mage\Adminhtml\Block\Widget\Grid.php(468): call_user_func(Array, Object(Mage_Catalog_Model_Resource_Product_Collection), Object(Mage_Adminhtml_Block_Widget_Grid_Column))
#8 C:\UwAmp\www\magento193\app\code\local\Mage\Adminhtml\Block\Catalog\Product\Grid.php(150): Mage_Adminhtml_Block_Widget_Grid->_addColumnFilterToCollection(Object(Mage_Adminhtml_Block_Widget_Grid_Column))
#9 C:\UwAmp\www\magento193\app\code\core\Mage\Adminhtml\Block\Widget\Grid.php(457): Mage_Adminhtml_Block_Catalog_Product_Grid->_addColumnFilterToCollection(Object(Mage_Adminhtml_Block_Widget_Grid_Column))
#10 C:\UwAmp\www\magento193\app\code\core\Mage\Adminhtml\Block\Widget\Grid.php(534): Mage_Adminhtml_Block_Widget_Grid->_setFilterValues(Array)
#11 C:\UwAmp\www\magento193\app\code\local\Mage\Adminhtml\Block\Catalog\Product\Grid.php(133): Mage_Adminhtml_Block_Widget_Grid->_prepareCollection()
#12 C:\UwAmp\www\magento193\app\code\core\Mage\Adminhtml\Block\Widget\Grid.php(643): Mage_Adminhtml_Block_Catalog_Product_Grid->_prepareCollection()
#13 C:\UwAmp\www\magento193\app\code\core\Mage\Adminhtml\Block\Widget\Grid.php(649): Mage_Adminhtml_Block_Widget_Grid->_prepareGrid()
#14 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Block\Abstract.php(922): Mage_Adminhtml_Block_Widget_Grid->_beforeToHtml()
#15 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Block\Abstract.php(641): Mage_Core_Block_Abstract->toHtml()
#16 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Block\Abstract.php(585): Mage_Core_Block_Abstract->_getChildHtml('grid', true)
#17 C:\UwAmp\www\magento193\app\code\core\Mage\Adminhtml\Block\Catalog\Product.php(79): Mage_Core_Block_Abstract->getChildHtml('grid')
#18 C:\UwAmp\www\magento193\app\design\adminhtml\default\default\template\catalog\product.phtml(46): Mage_Adminhtml_Block_Catalog_Product->getGridHtml()
#19 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Block\Template.php(241): include('C:\\UwAmp\\www\\ma...')
#20 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Block\Template.php(272): Mage_Core_Block_Template->fetchView('adminhtml\\defau...')
#21 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Block\Template.php(286): Mage_Core_Block_Template->renderView()
#22 C:\UwAmp\www\magento193\app\code\core\Mage\Adminhtml\Block\Template.php(81): Mage_Core_Block_Template->_toHtml()
#23 C:\UwAmp\www\magento193\app\code\core\Mage\Adminhtml\Block\Widget\Container.php(308): Mage_Adminhtml_Block_Template->_toHtml()
#24 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Block\Abstract.php(923): Mage_Adminhtml_Block_Widget_Container->_toHtml()
#25 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Block\Text\List.php(43): Mage_Core_Block_Abstract->toHtml()
#26 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Block\Abstract.php(923): Mage_Core_Block_Text_List->_toHtml()
#27 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Block\Abstract.php(641): Mage_Core_Block_Abstract->toHtml()
#28 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Block\Abstract.php(585): Mage_Core_Block_Abstract->_getChildHtml('content', true)
#29 C:\UwAmp\www\magento193\app\design\adminhtml\default\default\template\page.phtml(74): Mage_Core_Block_Abstract->getChildHtml('content')
#30 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Block\Template.php(241): include('C:\\UwAmp\\www\\ma...')
#31 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Block\Template.php(272): Mage_Core_Block_Template->fetchView('adminhtml\\defau...')
#32 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Block\Template.php(286): Mage_Core_Block_Template->renderView()
#33 C:\UwAmp\www\magento193\app\code\core\Mage\Adminhtml\Block\Template.php(81): Mage_Core_Block_Template->_toHtml()
#34 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Block\Abstract.php(923): Mage_Adminhtml_Block_Template->_toHtml()
#35 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Model\Layout.php(555): Mage_Core_Block_Abstract->toHtml()
#36 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Controller\Varien\Action.php(390): Mage_Core_Model_Layout->getOutput()
#37 C:\UwAmp\www\magento193\app\code\core\Mage\Adminhtml\controllers\Catalog\ProductController.php(180): Mage_Core_Controller_Varien_Action->renderLayout()
#38 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Controller\Varien\Action.php(418): Mage_Adminhtml_Catalog_ProductController->indexAction()
#39 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Controller\Varien\Router\Standard.php(254): Mage_Core_Controller_Varien_Action->dispatch('index')
#40 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Controller\Varien\Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#41 C:\UwAmp\www\magento193\app\code\core\Mage\Core\Model\App.php(365): Mage_Core_Controller_Varien_Front->dispatch()
#42 C:\UwAmp\www\magento193\app\Mage.php(684): Mage_Core_Model_App->run(Array)
#43 C:\UwAmp\www\magento193\index.php(83): Mage::run('', 'store')
#44 {main}";s:3:"url";s:87:"/magento193/index.php/admin/catalog_product/index/key/1cd021fdb902957ea3b63dfec61e2074/";s:11:"script_name";s:21:"/magento193/index.php";s:4:"skin";s:5:"admin";}

Can anyone help out here ?

Your Answer

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

Browse other questions tagged or ask your own question.