2

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?

8

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');
            }
        }

    }
  • using <br/> tag (in join method) is not recommended. You can use "\n" character instead. As i had some problem with my compiler i used <br/> tag – Shathish Jan 8 '14 at 4:47
  • great! it's works. you are rock (I replaced <br> with \n) but how can I remove or replace default columns "bill to name" and "ship to name"? – Lalit Kaushik Jan 9 '14 at 9:39
  • that depends on which method you used. If you used the first method, you can just comment out those two columns. If second method: in the observer use billing_name instead of full_address and shipping_name instead of full_address_ship. – Shathish Jan 9 '14 at 10:31
  • Sorry but it's not replaced. I am using second method. I tried this code: $tabBlock->addColumnAfter('billing_name', array( 'header'=> Mage::helper('sales')->__('Billing Address'), 'width' => '80px', 'type' => 'text', 'index' => 'full_address', ), 'created_at'); – Lalit Kaushik Jan 9 '14 at 12:42
  • did u replace all the instance of full_address and full_address_ship? can you post the screenshot of your grid? – Shathish Jan 10 '14 at 5:31
1

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.

  • Absolutely! I initially started to type my answer using observer, but changed my mind. I'll update my answer with that though :) – Shathish Jan 8 '14 at 4:20
  • How can i add custom attribute in sales order grid, magento.stackexchange.com/q/196004/57334 – zus Oct 24 '17 at 9:27
0

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',
            ));
  • Modifying core Magento files is strongly in conflict with best practices. – parhamr Nov 18 '15 at 23:39

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.