Sunday, October 23, 2016

Add Order Details Column in Customer Grid Magento


Many Magento website used for e-commerce website and for store owner customers are most important for any website. so they check all customer details who is online and who purchase maximum orders from the website. So store admin analysis stores customer for any offers or selling orders.
So here display basic code that you can overwrite exiting core block grid using new extension or you can directly customize admin customer grid using some code display here.
First open 'app/code/core/Mage/Adminhtml/Block/Customer/Grid.php' file or you can copy that file on local pool and than customize it. find function `_prepareCollection()` change code as below.

protected function _prepareCollection()
{
        $collection = Mage::getResourceModel('customer/customer_collection')
            ->addNameToSelect()
            ->addAttributeToSelect('email')
            ->addAttributeToSelect('created_at')
            ->addAttributeToSelect('group_id')
            ->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 below code for last order date
        $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));
  //Add below code for count total orders
  $sql ='SELECT COUNT(*) 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('orders_count'=>$expr));
  $this->setCollection($collection);
        return parent::_prepareCollection();
}

As below code you get the last order date and total customers orders from order table. In Magento `sales_flat_order` there is column for `customer_id` which is stored register customer id of that store.
Add below two fields in function `_prepareColumns ()` where you want to display fields.

$this->addColumn('last_order_date', array(
 'header'    => Mage::helper('customer')->__('Last Order Date'),
 'type'      => 'datetime',
 'align'     => 'center',
 'index'     => 'last_order_date',
 'filter'    => false, //For remove search column
 'sortable'  => false, //For remove sorting column
));
$this->addColumn('orders_count', array(
 'header'    => Mage::helper('customer')->__('Orders Count'),
 'index'     => 'orders_count',
 'type'  => 'number',
));
In below code you can use any `type` for show in grid and if you remove sorting and filtering functionality for this column than it's OK but if you want to short and filter of below column than you must create custom grid filter as below.

$this->addColumn('last_order_date', array(
 'header'    => Mage::helper('customer')->__('Last Order Date'),
 'type'      => 'datetime',
 'align'     => 'center',
 'index'     => 'last_order_date',
 'filter_condition_callback' => array($this, '_orderfiltersearch')
));
Than create new function on same file for search call callback function as below.

public function _orderfiltersearch($collection, $column)
{
 if (!$value = $column->getFilter()->getValue()){
  return $this;
 }
 if($value['orig_from'] == '' || $value['orig_to'] == ''){
  return $this;
 }
 $fromDate = date('Y-m-d H:i:s', strtotime($value['orig_from']));
 $toDate = date('Y-m-d H:i:s', strtotime($value['orig_to']));
 $orders = Mage::getModel('sales/order')->getCollection()->addAttributeToFilter('created_at', array('from'=>$fromDate, 'to'=>$toDate));
 foreach($orders as $order)
 {
  $customerids[] = $order->getCustomerId();
 }
 $customerids = array_unique($customerids);
 $this->getCollection()->addFieldToFilter('entity_id', array('in' => $customerids));
 return;
}
You can use same search callback function for, finding others with getting order collection from the database according to customer id and use that id in `addFieldToFilter` for filter Magento collection.

No comments :

Post a Comment