Question

I want to add complete address in default "Bill to Name" and "Ship to Name" columns in order grid in Magento admin. I am attaching screen-shot for more explanation. enter image description here Please suggest me how can I achieve this?

Was it helpful?

Solution

You need to extend the order grid (assuming that it is already familiar to you).

in the extended php file app/code/local/Yournamespace/Yourmodule/Block/Adminhtml/.../Grid.php

class Yournamespace_Yourmodule_Block_..._Grid extends Mage_Adminhtml_Block_Sales_Order_Grid{
    protected function _prepareCollection()
{
    $collection = Mage::getResourceModel($this->_getCollectionClass())
                    ->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');
    $this->setCollection($collection);
    return parent::_prepareCollection();
}

...

then inside protected function _prepareColumns() method replace the following code

    $this->addColumn('billing_name', array(
        'header' => Mage::helper('sales')->__('Bill to Name'),
        'index' => 'billing_name',
    ));

    $this->addColumn('shipping_name', array(
        'header' => Mage::helper('sales')->__('Ship to Name'),
        'index' => 'shipping_name',
    ));

with

    $this->addColumn('full_address', array(
        'header'=> Mage::helper('sales')->__('Billing Address'),
        'width' => '80px',
        'type'  => 'text',
        'index' => 'full_address',
    ));
    $this->addColumn('full_address_ship', array(
        'header'=> Mage::helper('sales')->__('Shipping Address'),
        'width' => '80px',
        'type'  => 'text',
        'index' => 'full_address_ship',
    ));

You're done!

The other easy and recommended way would be using event and observer:

in your module config.xml

under config tag...

<config>
    ...
    <adminhtml>
    ...
        <events>
            <sales_order_grid_collection_load_before>
                <observers>
                    <vendor>
                        <type>singleton</type>
                          <class>yourmodule/adminhtml_observer</class>
                        <method>filterOrderCollection</method>
                    </vendor>
                </observers>
            </sales_order_grid_collection_load_before>
            <core_block_abstract_prepare_layout_after>
                <observers>
                    <vendor>
                        <type>singleton</type>
                        <class>yourmodule/adminhtml_observer</class>
                        <method>saleOrderLayout</method>
                    </vendor>
                </observers>
            </core_block_abstract_prepare_layout_after>

in app/code/local/Yournamespace/Yourmodule/Model/Adminhtml/Observer.php

<?php
    class Yournamespace_Yourmodule_Model_Adminhtml_Observer{

        public function filterOrderCollection($observer){
            $collection = $observer->getEvent()->getOrderGridCollection();
            $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');
            $collection->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');
        }

        public function saleOrderLayout($evt){
            $tabBlock = $evt->getBlock();
            if($tabBlock instanceof Mage_Adminhtml_Block_Sales_Order_Grid){
                $tabBlock->addColumnAfter('full_address', array(
                'header'=> Mage::helper('sales')->__('Billing Address'),
                'width' => '80px',
                'type'  => 'text',
                'index' => 'full_address',
                ), 'created_at');
                $tabBlock->addColumnAfter('full_address_ship', array(
                'header'=> Mage::helper('sales')->__('Shipping Address'),
                'width' => '80px',
                'type'  => 'text',
                'index' => 'full_address_ship',
                ), 'full_address');
            }
        }

    }

OTHER TIPS

The best way to change the columns shown in the order grid is to use the observer that updates the sales_flat_order_grid table. In an earlier question I did a complete setup on how to extend the grid, see: Add Column to a grid (observer) - Column ‘store_id’ in where clause is ambiguous issue

In this example you have to change the customer_group_id with the addresses used. In the observer you will then have to add the collection of the data so that it gets saved to the grid table every time.

Alternatively you can use the renderer for the column to format and display the data for the address, but this requires that you do a lot of extra queries when loading the grid.

app/code/core/Mage/Adminhtml/Block/Sales/Order/Grid.php

$collection->getSelect()->joinLeft('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.parent_id',array('telephone','city','postcode' ) )->where("sales_flat_order_address.address_type = 'billing'");

           $this->addColumn('telephone', array(
            'header' => Mage::helper('sales')->__('Mobile'),
            'index' => 'telephone',
            'filter_index' => 'sales_flat_order_address.telephone',
            ));

            $this->addColumn('city', array(
            'header' => Mage::helper('sales')->__('City'),
            'index' => 'city',
            'filter_index' => 'sales_flat_order_address.city',
            ));

            $this->addColumn('postcode', array(
            'header' => Mage::helper('sales')->__('Pincode'),
            'index' => 'postcode',
            'filter_index' => 'sales_flat_order_address.postcode',
            ));
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top