14

How can I add a new column to the sales_flat_order_grid table and ensure that values are properly inserted there?

The "source" for the new column is a custom column we've added to sales_flat_order, let's call it foo. AFAICT, there are three ways to get foo to appear in the main order grid:

  1. JOIN the sales_flat_order_grid collection onto sales_flat_order.
    • Problem: Filters no longer work due to ambiguous columns (since both tables have similar column names)
  2. Have the grid use sales_flat_order for data instead of sales_flat_order_grid.
    • Problem: Columns aren't indexed, so filtering is dreadfully slow. It seems silly to add index the same data indexed in the unused sales_flat_order_grid table.
  3. Add a new column to sales_flat_order_grid and ensure the value updates there

I cannot figure out how sales_flat_order_grid is being updated, therefore I don't know how to add this new column. Any thoughts?

17

The sales_flat_order_grid table is updated at every order save action. You can add custom columns to the table in your own extension, if you add a column name that is already used in the sales_flat_order table you do not need to add anything extra, on every order-save action the columns are updated (if needed). If you want to insert data from a different table you will need to create an observer for the sales_order_resource_init_virtual_grid_columns event to collect and prepare the join.

For more details and a working example see my answer in Add Column to a grid (observer) - Column ‘store_id’ in where clause is ambiguous issue

  • Are you certain that this update is based solely on the column name? In my case, it didn't work until I deleted and re-created the attribute with "grid"=>true. Simply ALTERing both tables to add an identical column was not enough, nor was calling updateAttribute() on the existing attribute. (I got the idea from this question: stackoverflow.com/a/11254067/884734) – Eric Seastrand Apr 29 '15 at 17:12
  • With the grid=true attribute the setup script creates the attribue inobith the slas_flat_order and the sales_flat_order_grid table. This will make the update of an order save it to both tables. – Vladimir Kerkhoff Apr 29 '15 at 17:20
9

I have done same thing. Added 'order_type' field in order and displayed it in grid. It is working perfectly in Magento ver 1.7.0.2

How to add Order Type field in sales order grid in admin?

1) We have to create one install sql file with below code.

<?php 
/* @var $installer Mage_Sales_Model_Entity_Setup */
$installer = $this;
$installer->startSetup();
$installer->run(" 
ALTER TABLE `{$installer->getTable('sales/order')}` ADD `order_type` VARCHAR(255) NOT NULL;
ALTER TABLE `{$installer->getTable('sales/order_grid')}` ADD `order_type` VARCHAR(255) NOT NULL;
ALTER TABLE `{$installer->getTable('sales/quote')}` ADD `order_type` VARCHAR(255) NOT NULL; 
");
$installer->endSetup();
?>

2) Override Mage_Adminhtml_Block_Sales_Order_Grid file and add below code in it.

<?php

class Mycompany_Mymodule_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
    protected function _prepareColumns()
    {

        $options = array(
        'ordertypeID1' => 'order type label1',
        'ordertypeID2' => 'order type label2',
        'ordertypeID3' => 'order type label3',
        ); 
        $this->addColumn('order_type', array(
            'header'    =>  Mage::helper('customer')->__('Order Type'),
            'width'     =>  '100',
            'index'     =>  'order_type',
            'type'      =>  'options',
            'options'   =>   $options
        ));
        $this->addColumnsOrder('order_type', 'grand_total');
        return parent::_prepareColumns();
    }
}
?>

3) Create one observer event to add/update order type field value

Open your module/etc/config.xml

<config>
    <adminhtml> 
         <events>
            <adminhtml_sales_order_create_process_data>
                <observers>
                    <modulename>
                        <class>Mycompany_Mymodule_Model_Adminhtml_Observer</class>
                        <method>adminhtml_sales_order_create_process_data</method>
                    </modulename>
                </observers>
            </adminhtml_sales_order_create_process_data>
            <sales_convert_quote_to_order>
                <observers>
                    <modulename>
                        <type>model</type>
                        <class>Mycompany_Mymodule_Model_Adminhtml_Observer</class>
                        <method>sales_convert_quote_to_order</method> 
                    </modulename>
                </observers>
            </sales_convert_quote_to_order>
        </events>  
    </adminhtml>
</config>

4) Create one observer file of class Mycompany_Mymodule_Model_Adminhtml_Observer

<?php
class Mycompany_Mymodule_Model_Adminhtml_Observer 
{
    public function adminhtml_sales_order_create_process_data(Varien_Event_Observer $observer)
    { 
        try {
            $requestData = $observer->getEvent()->getRequest();

            if (isset($requestData['order']['order_type'])) {
                $observer->getEvent()->getOrderCreateModel()->getQuote()
                    ->addData($requestData['order']) 
                    ->save();
            } 

        } catch (Exception $e) {
            Mage::logException($e);
        }
        return $this;
    }


    /** 
     *
     * @param Varien_Event_Observer $observer
     * @return Mycompany_Mymodule_Model_Adminhtml_Observer
     */
    public function sales_convert_quote_to_order(Varien_Event_Observer $observer)
    {
        if ($ordertype = $observer->getEvent()->getQuote()->getOrderType()) {
            try {  
                $observer->getEvent()->getOrder()
                    ->setOrderType($ordertype);

            } catch (Exception $e) {
                Mage::logException($e);
            }
        }       

        return $this;
    }
}
?>
2

I used following similar code. It's working fine.

$installer = $this;
$installer->startSetup();

$installer->run("ALTER TABLE  sales_flat_order ADD COLUMN barcode  VARCHAR(255) NOT NULL;");

$installer->run("ALTER TABLE  sales_flat_order_grid ADD COLUMN barcode  VARCHAR(255) NOT NULL;");

$installer->run("ALTER TABLE  sales_flat_quote ADD COLUMN barcode  VARCHAR(255) NOT NULL;");

$installer->endSetup();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.