I am working on Magento 1.8.1 with a theme incorporated. Multiple extensions involved as well and most are completely customized.
I have been trying to get the company name to appear on the Sales Order Grid for some time now and have had some luck.
First off, I have copied /core/Mage/Adminhtml/Block/Sales/Order/Grid.php
to /local/Mage/Adminhtml/Block/Sales/Order/Grid.php
.
Then I updated the code in _prepareCollection()
to this:
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->join(
array('addressTable' => 'sales_flat_order_address'),'main_table.entity_id = addressTable.parent_id AND addressTable.address_type = "billing"',array('billing_company'=>'company'));
$this->setCollection($collection);
return parent::_prepareCollection();
}
While adding the following to _prepareColumns:
$this->addColumn('company', array(
'header' => Mage::helper('sales')->__('Bill to Company'),
'index' => 'billing_company',
));
Everything is working like it should until I decided it would be really nice to have the Ship to Company as well since most of our customers buy for other companies.
To accomplish this I added the column with no problem just like I did previously:
$this->addColumn('company', array(
'header' => Mage::helper('sales')->__('Ship to Company'),
'index' => 'shipping_company',
));
The column added with no problem except it was not in the place I put it (before shipping_name
), This was just adding the column no data yet.
Now to add the data, I would add another collection under _prepareCollection
since the shipping information is on a different row compared to billing in the table, like this:
$collection->getSelect()->join(
array('addressTable' => 'sales_flat_order_address'),'main_table.entity_id = addressTable.parent_id AND addressTable.address_type = "shipping"',array('shipping_company'=>'company'));
But when I try this I get an error:
You cannot define a correlation name 'addressTable' more than once
I get quite a bit of conflict between the two columns as well. For instance, when I do not have the Ship to Company commented under _prepareColumns
it shows the Ship to Company column where the Bill to Company column should be. There is also no data in the column. As soon as I comment Ship to Company Bill to Company appears and has the correct data.
Basically, I just need to get the Ship to Company Column to show up as well as the Bill to Company Column. Preferably next to the appropriate Name.
I have already added the Company Column to the Customer Grid and Create New Order Customer Grid with no problem as well.
Okay so my edit above was not exactly correct. The columns show up and populate just fine but after trying to search the field I was receiving an error. So after more research, I found this solution: Using column aliases in Sales Order Grid field
The answer to this question works perfect just had to change a few minor things. I attached my code below just in case someone else is looking for this solution.
protected function _prepareCollection()
{
$collection = Mage::getResourceModel($this->_getCollectionClass());
$collection->getSelect()->join(array('address_billing' => $collection->getTable("sales/order_address")),'main_table.entity_id = address_billing.parent_id AND address_billing.address_type = "billing"',array('address_billing.company as billing_company'));
$collection->getSelect()->join(array('address_shipping' => $collection->getTable("sales/order_address")),'main_table.entity_id = address_shipping.parent_id AND address_shipping.address_type = "shipping"',array('address_shipping.company as shipping_company'));
$this->setCollection($collection);
return parent::_prepareCollection();
}
Under _prepareColumns() change to this:
$this->addColumn('company', array(
'header' => Mage::helper('sales')->__('Bill to Company'),
'index' => 'billing_company',
'filter_index' => 'address_billing.company',
));
$this->addColumnAfter('shipping_company', array(
'header' => Mage::helper('sales')->__('Ship to Company'),
'index' => 'shipping_company',
'filter_index' => 'address_shipping.company',
),
'billing_name'
);
Remember I had to use the addColumnAfter function to get my shipping company column placed in the right spot.
With this fix everything is finally working the way I want it to!
Happy coding!