3

I have add a custom column in customer grid by copying core Grid.php file to local folder. I want to show last order date of customer in a column. So I have tried below code in collection.

protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel('customer/customer_collection')
            ->addNameToSelect()
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('group_id')
            ->addAttributeToSelect('track_device')
            ->addAttributeToSelect('inchoo_socialconnect_fid')
            ->addAttributeToSelect('inchoo_socialconnect_gid')
            ->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
            ->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
            ->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
            ->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
            ->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');


       // add last_order_date column     
            $sql ='SELECT MAX(o.created_at)'
                . ' FROM ' . Mage::getSingleton('core/resource')->getTableName('sales/order') . ' AS o'
                . ' WHERE o.customer_id = e.entity_id ';
            $expr = new Zend_Db_Expr('(' . $sql . ')'); 

            $collection->getSelect()->from(null, array('last_order_date'=>$expr));


        $this->setCollection($collection);

        return parent::_prepareCollection();
    }

here is the column code--

$this->addColumn('last_order_date', array(
            'header'    => Mage::helper('customer')->__('Last Order Date'),
            'type'      => 'datetime',
            'align'     => 'center',
            'index'     => 'last_order_date',
            'filter'    => true,
            'sortable'  => true ,               
            'gmtoffset' => true,        
        ));

column showing correct values but Sorting and Filtering not working for this column. What is wrong there.

2

Since last_order_date is a real field you will need to implement filter_condition_callback

$this->addColumn('last_order_date',
        array(
            ....
            'filter_condition_callback' => array($this, '_filterLastOrderDate'),
    ));

See Magento - custom column in AdminHtml product grid which contains custom output/logic?

  • is last_order_date exist in any table by default. I don't know about this if it is exist already. – Shashank Kumrawat Feb 23 '16 at 14:39
  • I am getting main problem in sorting this column. i don't know how to do it? – Shashank Kumrawat Feb 23 '16 at 14:48
  • Is there a reason you can not use a join / group by? – Renon Stewart Feb 23 '16 at 14:57
  • I don't know where to apply join/group in above code. – Shashank Kumrawat Feb 23 '16 at 14:59
  • can you provide some idea.? – Shashank Kumrawat Feb 23 '16 at 15:23

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.