Magento – how to add customer’s shipping address detail in Admin > Sales > Order grid

adminhtmlgridmagento-1.9order-grid

I trying to add customer's shipping address detail in Admin > Sales > Order grid
with following code

in protected function _prepareCollection()

$collection->getSelect()->join(array('Shipping'=>'sales_flat_order_address'),
'main_table.entity_id=Shipping.parent_id 
and Shipping.address_type="Shipping"',array('city','street','telephone','postcode','region'));

in protected function _prepareColumns()

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

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

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

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

but showing blank columns

Best Answer

The sales_flat_order_address address table can have 2 rows for each order so you can't directly join this table with the sales_flat_order_grid table, you need to join against the sales_flat_order table first and then join the sales_flat_order_address table using the shipping_address_id or the billing_address_id:

$select = $collection->getSelect();
$resource = Mage::getSingleton('core/resource');
$select->join(
    array('order' => $resource->getTableName('sales/order')),
    'main_table.entity_id = order.entity_id'
);

$select->join(
    array('billing' => $resource->getTableName('sales/order_address')),
    'order.billing_address_id = billing.entity_id',
    array('billing_city' => 'city', 'billing_street' => 'street', 'billing_telephone' => 'telephone', 'billing_postcode' => 'postcode', 'billing_region' => 'region')
);

$select->joinLeft(
    array('shipping' => $resource->getTableName('sales/order_address')),
    'order.shipping_address_id = shipping.entity_id',
    array('shipping_city' => 'city', 'shipping_street' => 'street', 'shipping_telephone' => 'telephone', 'shipping_postcode' => 'postcode', 'shipping_region' => 'region')
);

And then add the columns as follows:

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

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

// add whatever extra columns you need from those selected
Related Topic