Without knowing exactly what your extension will do it's hard to say which direction I should recommend you go. But, generally, iI believe that it is best practice to extend and add columns to sales_flat_*
tables, as evidenced by Magento's own extensions that they have implemented.
For instance, in Enterprise Edition instead of creating join tables for sales data on Enterprise_GiftWrapping, for instance, they extend sales_flat_order
and sales_flat_order_item
to include relevant gw_*
fields to contain sales information as needed.
In my opinion there is little to no danger in making your edits to these tables on a 1.x install of Magento - unless, of course, you're wanting to support legacy Magento versions in which case sales data was EAV until ~1.4CE.
A potential argument against using join tables is that any join in the admin that keys against a flat table used for order placement (such as sales_flat_order
) has the potential of affecting a lock upon that table which may cause unintended problems for high-volume stores.
I had an admin sort by customer_email in the order grid view (a custom extension) - the email address is unindexed and the sort was on > 300k rows. This caused 3 orders to fail with a lock wait timeout, but payments had been made and authorized in the gateway. What's worse is that the customers were then unable to complete their order subsequently due to duplicate transaction id numbers, a limitation of the gateway.
So, proceed with caution... but in my opinion you're clear to edit the core tables.
This issue you can solve by customizing the renderer file. You can fetch the format from database by custom query as well.
For eg.
Prodid.php
<?php
class Mage_Adminhtml_Block_Sales_Order_Renderer_Prodid extendsMage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
public function render(Varien_Object $row){
$url = $this->getUrl('*/*/edit', array(
'store'=>$this->getRequest()->getParam('store'),
'id'=>$row->getId())
);
$exploding = explode('sales_order/edit/id/',$url);
//print_r($exploding);
$id = explode('/',$exploding[1]);
//print_r($id);
$myId = $id[0];
//echo $myId;
$write = Mage::getSingleton('core/resource')->getConnection('core_write');
// now $write is an instance of Zend_Db_Adapter_Abstract
$readresult=$write->query("SELECT product_id FROM `sales_flat_order_item` WHERE `order_id` =$myId ");
while ($row = $readresult->fetch() ) {
$productid = $row['product_id'];
$myprodid .= $productid.',';
}
$myprodid2 = substr($myprodid,0,-1);
return $myprodid2;
}
}
?>
This will give you the desired result in my case it was product id
Then calling it in the grid file would be like
$this->addColumn('ids', array(
'header' => Mage::helper('sales')->__('Product ID'),
'width' => '100px',
'index' => 'ids',
'type' => 'text',
'filter' => false,
'sortable' => false,
'renderer' => 'Mage_Adminhtml_Block_Sales_Order_Renderer_Prodid'
));
The filter=false and sortable false will remove the a href from the headers and will only show you the data.
If you want to make them sortable, then you will need to pass the corresponding unique index id.
Please share if anything is unclear here.
Best Answer
If I do understand correctly you would like to have two "true" separated columns.
Here is a quick solution.
Join the billing street two times in
_prepareCollection()
of your grid so you can use them as separate columns:Define the two columns in
_prepareColumns()
of your grid:Notice that I disabled sorting on the second line as this isn't possible with my suggestion. With a little bit more time you may be able to work out a sortable solution, though.
Add the two filter condition callback methods in your grid in order to be able to filter for the columns. The syntax may be MySQL-specific but you may get away with it as long as you don't plan to use other databases:
And finally, create the two renderer classes for rendering the first line in the one column and the second line in the other column. Let's assume that your Extension is called
Mzeis_Grid
and has been configured to be in thelocal
code pool. In theconfig.xml
you write:Your first column renderer,
app/code/local/Mzeis/Grid/Block/Adminhtml/Widget/Grid/Column/Renderer/Address/Firstline.php
:Your second column renderer,
app/code/local/Mzeis/Grid/Block/Adminhtml/Widget/Grid/Column/Renderer/Address/Secondline.php
: