PHP Magento Tips

Adding a Custom Attribute or Column to the Sales Orders Grid in Magento admin

Rate this posting:
{[['']]}

Does your Magento order grid have all of the fields you need? Adding new columns or attributes is fairly simple. Let’s explore how.

We are going to first copy the core file to a local directory, then code a bit. Copy app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php to app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php. First, to make custom attribute available for inserting to grid we need to modify _prepareCollection function. Here, we are working with database table fields and using SQL joins.

In this demo code i added customer email, shipping/billing postcode, payment method, customized billing and shipping address. to do this Search for _prepareCollection function and modify your code as per below (Check highlighted lines in below code).

protected function _prepareCollection()
    {
        $collection = Mage::getResourceModel($this->_getCollectionClass());
         
        // customized billing and shipping address
        $collection->join(array('soa' => 'sales/order_address'), 'soa.parent_id=main_table.entity_id and soa.address_type = "billing"', array('full_address'=>'CONCAT(soa.firstname, " " , soa.lastname, ",<br/>", soa.street, ",<br/>", soa.city, ",<br/>", soa.region, ",<br/>", soa.postcode)' ), null,'left')
        ->join(array('soas' => 'sales/order_address'), 'soas.parent_id=main_table.entity_id and soas.address_type = "shipping"', array('full_address_ship'=>'CONCAT(soas.firstname, " " , soas.lastname, ",<br/>", soas.street, ",<br/>", soas.city, ",<br/>", soas.region, ",<br/>", soas.postcode)' ), null,'left');
        
        // get customer email
        $collection->getSelect()->joinLeft(
                array('myorder'=>'sales_flat_order'),
                'myorder.entity_id = main_table.entity_id',
                array('myorder.customer_email')
        );
        
        //get payment method
        $collection->getSelect()->join('sales_flat_order_payment', 'main_table.entity_id = sales_flat_order_payment.parent_id',array('method') );
        
        //get shipping postcode
        $collection->getSelect()->joinLeft(
                array('shipping'=>'sales_flat_order_address'),
                'main_table.entity_id = shipping.parent_id AND shipping.address_type="shipping"',
                array('shipping.postcode AS sp'));
    
        $collection->getSelect()->group('main_table.entity_id');
        $this->setCollection($collection);
        return parent::_prepareCollection();
    } 

As you can see, we have joined the SQL query. To insert this column into the order grid, edit _prepareColumns() function. Here you can see how default columns are inserted, so using the addColumn method, we add our new columns.

protected function _prepareColumns()
    {

        $this->addColumn('real_order_id', array(
            'header'=> Mage::helper('sales')->__('Order #'),
            'width' => '80px',
            'type'  => 'text',
            'index' => 'increment_id',
        ));

        if (!Mage::app()->isSingleStoreMode()) {
            $this->addColumn('store_id', array(
                'header'    => Mage::helper('sales')->__('Purchased From (Store)'),
                'index'     => 'store_id',
                'type'      => 'store',
                'store_view'=> true,
                'display_deleted' => true,
            ));
        }

        $this->addColumn('created_at', array(
            'header' => Mage::helper('sales')->__('Purchased On'),
            'index' => 'created_at',
            'type' => 'datetime',
            'width' => '100px',
        ));

        /*$this->addColumn('billing_name', array(
            'header' => Mage::helper('sales')->__('Bill to Name'),
            'index' => 'billing_name',
        ));*/
        $this->addColumn('full_address', array(
                'header'=> Mage::helper('sales')->__('Billing Address'),
                'width' => '80px',
                'type'  => 'text',
                'index' => 'full_address',
        ));
        $this->addColumn('customer_email', array(
                'header' => $this->helper('sales')->__('Customer Email'),
                'index' => 'customer_email',
                'filter_index' => 'myorder.customer_email',
        ));
        $this->addColumn('sp', array(
        'header' => Mage::helper('sales')->__('Shipping Postcode'),
        'index' => 'sp',
        'width' => '60px',
        'filter_index'  => 'shipping.postcode'
        ));
        $this->addColumn('method', array(
                'header' => Mage::helper('sales')->__('Method'),
                'index' => 'method',
                'width' => '80px',
                'filter_index' => 'sales_flat_order_payment.method',
        ));
        /*$this->addColumn('shipping_name', array(
            'header' => Mage::helper('sales')->__('Ship to Name'),
            'index' => 'shipping_name',
        ));*/
        $this->addColumn('full_address_ship', array(
                'header'=> Mage::helper('sales')->__('Shipping Address'),
                'width' => '80px',
                'type'  => 'text',
                'index' => 'full_address_ship',
        ));
        
        $this->addColumn('base_grand_total', array(
            'header' => Mage::helper('sales')->__('G.T. (Base)'),
            'index' => 'base_grand_total',
            'type'  => 'currency',
            'currency' => 'base_currency_code',
        ));

        $this->addColumn('grand_total', array(
            'header' => Mage::helper('sales')->__('G.T. (Purchased)'),
            'index' => 'grand_total',
            'type'  => 'currency',
            'currency' => 'order_currency_code',
        ));
  
        $this->addColumn('status', array(
            'header' => Mage::helper('sales')->__('Status'),
            'index' => 'status',
            'type'  => 'options',
            'width' => '70px',
            'options' => Mage::getSingleton('sales/order_config')->getStatuses(),
        ));

        if (Mage::getSingleton('admin/session')->isAllowed('sales/order/actions/view')) {
            $this->addColumn('action',
                array(
                    'header'    => Mage::helper('sales')->__('Action'),
                    'width'     => '50px',
                    'type'      => 'action',
                    'getter'     => 'getId',
                    'actions'   => array(
                        array(
                            'caption' => Mage::helper('sales')->__('View'),
                            'url'     => array('base'=>'*/sales_order/view'),
                            'field'   => 'order_id',
                            'data-column' => 'action',
                        )
                    ),
                    'filter'    => false,
                    'sortable'  => false,
                    'index'     => 'stores',
                    'is_system' => true,
            ));
        }
        $this->addRssList('rss/order/new', Mage::helper('sales')->__('New Order RSS'));

        $this->addExportType('*/*/exportCsv', Mage::helper('sales')->__('CSV'));
        $this->addExportType('*/*/exportExcel', Mage::helper('sales')->__('Excel XML'));

        return parent::_prepareColumns();
    } 
Tag : Magento
1 Comments On "Adding a Custom Attribute or Column to the Sales Orders Grid in Magento admin"

I really appreciate information shared above. It’s of great help. If someone want to learn Online (Virtual) instructor lead live training in Typo3, kindly contact us http://www.maxmunus.com/contact
MaxMunus Offer World Class Virtual Instructor led training on Typo3. We have industry expert trainer. We provide Training Material and Software Support. MaxMunus has successfully conducted 100000+ trainings in India, USA, UK, Australlia, Switzerland, Qatar, Saudi Arabia, Bangladesh, Bahrain and UAE etc.
For Demo Contact us:
Name : Arunkumar U
Email : arun@maxmunus.com
Skype id: training_maxmunus
Contact No.-+91-9738507310
Company Website –http://www.maxmunus.com


Back To Top