Magento – Adding a custom field to Order admin screen

magento-1.7orders

I'm trying to add a custom field to the order admin screen, specifically a dropdown box with the information appended to the order.

I have created a custom module, which has the structure:

etc -> with config.xml

    <?xml version="1.0"?>
<config>
    <modules>
        <Custom_Module>
            <version>0.4.0</version>
        </Custom_Module>
    </modules>

    <global>
     <resources>
         <Custom_Module>
              <setup>
                  <module>Custom_Module</module>
                  <class>Mage_Sales_Model_Mysql4_Setup</class>
              </setup>
               <connection>
                <use>core_setup</use>
              </connection>
         </Custom_Module>
     </resources>
 </global>
</config>

sql -> Custom_Module_Setup/mysql4-install-0.4.0.php

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

$options = array(
    'value' = array(
        'test' => array('test'),
        'test2' => array('test2'),
        )
    );

$installer->addAttribute('order', 'test_option', array(
    'label' => 'Test Option',
    'default' => '',
    'type' => 'varchar',
    'input' => 'select',
    'visible' => true,
    'required' => false,
    'position' => 1,
    'visible_on_front'  => false,
    'option' => $options,
    'default' => array('web'),
));

$installer->endSetup();

I also have in modules

<?xml version="1.0"?>
<config>
    <modules>
        <Custom_Module>
            <active>true</active>
            <codePool>local</codePool>
            <depends></depends>
        </Custom_Module>
    </modules>
</config>

From what I can tell, the module is activated, and config file is read but the install file is not?
The attribute is not appearing as a column in sales_flat_order (I have been flushing the cache while testing each time)

So question 1 what am I doing wrong?
Question 2 once I have it appearing in the column, how can I then display/save it in the order edit screen?

Best Answer

You have an error in your config.xml, change:

<global>
 <resources>
     <Custom_Module>
          <setup>
              <module>Custom_Module</module>
              <class>Mage_Sales_Model_Mysql4_Setup</class>
          </setup>
           <connection>
            <use>core_setup</use>
          </connection>
     </Custom_Module>
 </resources>

To:

<resources>
        <custom_module_setup>
            <setup>
                <module>Custom_Module</module>
        <class>Mage_Sales_Model_Mysql4_Setup</class>
            </setup>
        </custom_module_setup>
    </resources>

And then create the upgrade script in the directory: sql/custom_module_setup/, since 1.6 you can name them install-0.4.0.php (without the mysql4- prefix). Also check the core_resource tabel for custom_module_setup to check what version is installed, remove the record if you want your setup script to run again.

Related Topic