13

I have to add a column to the orders page which means adding a column to sales_flat_order_grid. Both of those are possible however I don't know how to add a value so that it gets saved in my new column.

Do I have to register a new attribute too?
The value is not present in the quote so I guess I don't need to register under config/global/fieldsets/sales_convert_quote since the value isn't there to be converted.

Using Magento Enterprise 1.8.

clockworkgeek
  • 37,650
  • 9
  • 89
  • 127

3 Answers3

23

After a lot of trial and error - a lot of error - I think I have it now.

To begin with the sales_flat_order_grid is updated in Mage_Sales_Model_Mysql4_Order_Abstract::updateGridRecords(), by following the trail I worked out it inspects both the "main" table (sales_flat_order) and the main table + "_grid" (sales_flat_order_grid), takes the intersect of their columns and constructs a query from that. So any column you need in the grid table must also be in the main table. It's not an EAV-style entity so attributes don't need to be created.
Here is my setup script:

<?php

/* @var $this Nexxt_Booth_Model_Entity_Setup */
$installer = $this;

$installer->getConnection()->addColumn($installer->getTable('sales_flat_order'), 'box_num', 'varchar(255)');
$installer->getConnection()->addColumn($installer->getTable('sales_flat_order_grid'), 'box_num', 'varchar(255)');

Next, I needed the extra column to show in all order tables in admin. To do this I overrode each relevant block.

<?xml version="1.0"?>
<config>
    ....

    <global>
        <blocks>
            <adminhtml>
                <rewrite>
                    <customer_edit_tab_view_orders>
                    <!-- Recent 5 orders on customer page -->
                        My_Module_Block_Adminhtml_Customer_Edit_Tab_View_Orders
                    </customer_edit_tab_view_orders>
                    <customer_edit_tab_orders>
                    <!-- All orders on customer tab -->
                        My_Module_Block_Adminhtml_Customer_Edit_Tab_Orders
                    </customer_edit_tab_orders>
                    <sales_order_grid>
                    <!-- All orders in Sales menu -->
                        My_Module_Block_Adminhtml_Sales_Order_Grid
                    </sales_order_grid>
                </rewrite>
            </adminhtml>
        </blocks>
    </global>
</config>

In My/Module/Block/Adminhtml/Sales/Order/Grid.php I made the following:

<?php

class My_Module_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid

    protected function _prepareColumns()
    {
        $this->addColumn('box_num', array(
            'header'    => $this->__('Box #'),
            'index'     => 'box_num',
            'width'     => '100px'
        ));
        $this->addColumnsOrder('box_num', 'shipping_name');
        return parent::_prepareColumns();
    }

}

Similarly, in My/Module/Block/Adminhtml/Customer/Edit/Tab/Orders.php and My/Module/Block/Adminhtml/Customer/Edit/Tab/View/Orders.php I added this function:

    protected function _prepareColumns()
    {
        $this->addColumn('box_num', array(
            'header'    => $this->__('Box #'),
            'index'     => 'box_num',
            'width'     => '100px'
        ));
        $this->addColumnsOrder('box_num', (Mage::app()->isSingleStoreMode() ? 'grand_total' : 'store_id'));
        return parent::_prepareColumns();
    }

Finally, to finish off, in the event sales_convert_quote_to_order I populated the new field. This bit isn't as important, so long as you add the data at a point before the order is saved.

$order->setBoxNum('DATA GOES HERE');
clockworkgeek
  • 37,650
  • 9
  • 89
  • 127
  • I am trying to do exactly the same, I have made a new column in sales_flat_order_grid as barcode_no and using Grid.php I have displayed that column in magento orders grid. Now how can I store barcode_no value from magento backend to sales_flat_order_grid table ? – Shujaat Shaikh Feb 12 '18 at 07:58
  • @ShujaatShaikh which bit are you having trouble with? – clockworkgeek Feb 13 '18 at 09:46
  • No where I can find a tuturial, explaining how to insert a custom value to my php database from magento admin backend. Any article would be great help! – Shujaat Shaikh Feb 13 '18 at 14:04
  • I think you want to add an input to the order view page. That's too complicated to explain here. It deserves an entire question by itself. I suggest start by looking at the gift message box and try to understand all the things it requires. It has a form on the order page, and an AJAX script, and a controller which updates the order. – clockworkgeek Feb 14 '18 at 12:45
1

you have to add attributes , best way to do so is via your extensions update or setup script. As the order and quote are based on flat table structures it really means adding more fields to those tables

also see ALTER TABLE in Magento setup script without using SQL

Community
  • 1
  • 1
Anton S
  • 12,750
  • 2
  • 35
  • 37
0

Also, you can use this for adding the new "attribute" (at really is a column because is flat) to both, the sales_flat and the sales_flat_ _grid

$installer->addAttribute('order', 'box_num', array(
'label'    => 'Box Number',
'type'     => 'varchar',
'grid'     => true      // this is important
));

your installer must be: Mage_Sales_Model_Entity_Setup for using the grid key.

And remember if you are adding an existent columnd from sales_flat_order to sales_flat_order_grid, then you need to resync the sales_flat_order_grid table:

In Magento, how do I populate a new column in sales_order_grid with data from sales_flat_order?

Community
  • 1
  • 1
Enrique
  • 4,693
  • 5
  • 51
  • 71
  • In my case, I had to use the `Mage_Sales_Model_Resource_Setup` class for this. Creating attributes with the `Mage_Sales_Model_Entity_Setup` class seemed to do nothing (no new db columns created, but no errors either.) – Eric Seastrand Apr 29 '15 at 17:17