Using Magento 1.8.1
I've added custom columns to the Sales Order Grid. The collection is as follows in my app/code/local/Mage/Adminhtml/Block/Sales/Order/Grid.php:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$this->setCollection($collection);
$collection->getSelect()->join('sales_flat_order', 'main_table.entity_id = sales_flat_order.entity_id', array('base_shipping_amount', 'base_subtotal', 'base_tax_amount'));
return parent::_prepareCollection();
}
Now that is working perfectly fine, I want to append a new column from a different table, the sales_flat_order_address table, but I'm having trouble. How do I effectively add in a second collection into the _prepareCollection method? Basically I want to add in:
$collection->getSelect()->join('sales_flat_order_address', 'main_table.entity_id = sales_flat_order_address.entity_id', array('country_id'));
Into the first collection so that I can use country_id in the columns with the other data.
Best Answer
The flat orders table contains the billing and shipping address id's so you should join against these instead of using
entity_id
and filtering byshipping
orbilling
. Also not all orders will have an associated shipping address (i.e downloadable products) so you should use a left join to ensure you don't remove these orders from the grid. Finally, it's best practice to use the propergetTableName()
method to pull the table names and also alias table names. Essentially you end up with:You can then use
billing_country
andshipping_country
to add the columns in your_prepareColumns()
method. Addingfilter_index
ensures the correct table and column is referenced when sorting and searching: