# Adding custom fields to sales_flat_order

I've been doing a fair amount of googling, trial and error but I cannot find a solution to the problem.

1. The ability to change the fields and order of sales_order_grid; and
2. The ability to display two custom fields on this grid (filterable).

The former (point 1) has been solved, by extending the Mage_Adminhtml_Block_Widget_Grid in my custom module (I know about observers, yet other installed modules were overriding my changes with their observers).

Regardless, the latter is my current problem, below are the two methods that have failed me so far.

# Method 1

<?php
/*  @var $this Mage_Sales_Model_Mysql4_Setup */$this->startSetup();
$connection =$this->getConnection();

/**
* Create the payment method dropdown field, because this field _may_ be
* used for searching we will create an index for it.
*/
$connection->addColumn($this->getTable('sales/order_grid'),
'x_payment_method',
"ENUM('PayPal', 'SagePay') DEFAULT NULL"
);
$connection->addKey($this->getTable('sales/order_grid'), 'x_payment_type', 'x_payment_type');

/**
* Create the order channel field to identify where the order was originally
* generated from. Also add an index for this field for additional filtering.
*/
$connection->addColumn($this->getTable('sales/order_grid'),
'x_sale_channel',
"ENUM('Amazon', 'Play', 'eBay', 'Website') NOT NULL DEFAULT 'Website'"
);
$connection->addKey($this->getTable('sales/order_grid'), 'x_sale_channel','x_sale_channel');

$this->endSetup();  # Method 2 By this point I was tired of reading the same 7 articles that didn't help, so I tried to get ONE field working; I also checked the error logs in Magento and found "$this->getTable()" was errornous, thus I removed it.

<?php
/*  @var $this Mage_Sales_Model_Mysql4_Setup */$this->startSetup();
$connection =$this->getConnection();

/**
* Create the payment method dropdown field, because this field _may_ be
* used for searching we will create an index for it.
*/
$this->addAttribute('sales_flat_order', 'x_test_option', array( 'label' => 'X Test Option', 'type' => 'varchar', 'input' => 'select', 'visible' => true, 'required' => false, 'position' => 1, 'visible_on_front' => false, 'option' => array('value' => array('web', 'test 1', 'test 2')), 'default' => array('web'), ));$this->endSetup();


Which makes one beg the question, what is the difference between a column and an attribute? My initial presumption was that, a column is added to an existing core table while an attribute is added to the EAV_* tables and appropriately related.

• Even though that table is flat, the resource model for it is EAV, hence the attribute has to be registered in the EAV config to be written to. Several sales entities used to be EAV but were flattened out for performance reasons. – benmarks Oct 21 '13 at 13:00

But for the sales_ table they are one and the same.
For any other flat tabled entities (cms pages, blocks, polls) you cannot use addAttribute, only addColumn, but for sales it works both ways.
If you plan to make an extension that has to be compatible with versions before 1.4 then use addAttribute, otherwise I see no point in it.
• Thanks for the info I find this really helpful. Actually, now thinking about it, I suppose the right way is to use addColumn method? Whilst also not using 'getTable' as the sales_flat_order is just that. I will try that out shortly, and return with a virdict :) – ash Oct 21 '13 at 11:34
• It turns out I had two problems (one I overlooked); One was $this->getTable('sales/flat_order') may as well be set to sales_flat_order; the second was $connection->addKey(\$this->getTable('sales/order_grid'), 'x_payment_type', 'x_payment_type');; x_payment_type should have been x_payment_method. – ash Oct 21 '13 at 12:11