title: Magento custom "order" attribute / admin input and display
body: <p>I need to add a custom attribute to the order model ("<strong>dynamics_ord</strong>"). </p>
  
  <p>This attr needs to be defined by admin staff, after the order has been successfully saved, ideally on the <strong>admin/sales_order/view</strong>, in the main order block. </p>
  
  <p>It needs to be independent of invoice / shipment etc - maybe attached to an order "<code>edit</code>" event.</p>
  
  <p>I've made a number of custom attributes for other entities (notably - customers). And I realize this is the same process. But I'm not clear how to go about amending an order with this attribute, as there is no obvious "update" action. "Edit" may make sense, but I'd rather not duplicate orders needlessly. </p>
  
  <p>Once this attr is in place, I want to display it in the <strong>sales_order/index</strong> grid.
  (Note = check out <a href="https://stackoverflow.com/questions/14695352/magento-add-custom-attribute-column-to-sales-order-index-grid">this related thread</a> for a working solution.)</p>
  
  <p><strong>Final State of this module</strong> (and for anyone else in need of this solution): </p>
  
  <p><strong>/sales_order/view/</strong> screenshot :
  <img src="https://i.stack.imgur.com/BaEbb.jpg" alt="Screenshot"></p>
  
  <p><strong>WACI_SalesExt</strong></p>
  
  <pre><code>app/local/WACI/SalesExt
   /Block
     /Adminhtml
       /Sales
         -Dynamicsord.php
   /controllers
     /Adminhtml
       /Sales
         - OrderController.php
   /etc
    - config.xml
   /Helper
     - Data.php
   /sql
     /waci_salesext_setup
       -mysql4-install-0.2.0.php
  </code></pre>
  
  <p><strong>config.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;config&gt;
      &lt;modules&gt;
          &lt;WACI_SalesExt&gt;
              &lt;version&gt;0.2.0&lt;/version&gt;
          &lt;/WACI_SalesExt&gt;
      &lt;/modules&gt;
      &lt;global&gt;
  
          &lt;helpers&gt;
              &lt;WACI_SalesExt&gt;
                  &lt;class&gt;WACI_SalesExt_Helper&lt;/class&gt;
              &lt;/WACI_SalesExt&gt;
          &lt;/helpers&gt;
  
          &lt;blocks&gt;
              &lt;WACI_SalesExt&gt;
                  &lt;class&gt;WACI_SalesExt_Block&lt;/class&gt;
              &lt;/WACI_SalesExt&gt;
  
              &lt;adminhtml&gt; &lt;!-- unclear if i need this second declaration for admin--&gt;
                  &lt;WACI_SalesExt&gt;
                      &lt;class&gt;WACI_SalesExt_Block&lt;/class&gt;
                  &lt;/WACI_SalesExt&gt;
              &lt;/adminhtml&gt;
  
          &lt;/blocks&gt;
  
          &lt;models&gt;
              &lt;WACI_SalesExt&gt;
                  &lt;class&gt;WACI_SalesExt_Model&lt;/class&gt;
                  &lt;resourceModel&gt;salesext_mysql4&lt;/resourceModel&gt;
              &lt;/WACI_SalesExt&gt;
  
          &lt;/models&gt;
  
          &lt;resources&gt;
              &lt;waci_salesext_setup&gt;
                  &lt;setup&gt;
                      &lt;module&gt;WACI_SalesExt&lt;/module&gt;
                      &lt;class&gt;Mage_Sales_Model_Mysql4_Setup&lt;/class&gt;
                  &lt;/setup&gt;
                  &lt;connection&gt;
                      &lt;use&gt;core_setup&lt;/use&gt;
                  &lt;/connection&gt;
              &lt;/waci_salesext_setup&gt;
              &lt;waci_salesext_write&gt;
                  &lt;connection&gt;
                      &lt;use&gt;core_write&lt;/use&gt;
                  &lt;/connection&gt;
              &lt;/waci_salesext_write&gt;
              &lt;waci_salesext_read&gt;
                  &lt;connection&gt;
                      &lt;use&gt;core_read&lt;/use&gt;
                  &lt;/connection&gt;
              &lt;/waci_salesext_read&gt;
          &lt;/resources&gt;
  
      &lt;/global&gt;
  
  &lt;admin&gt;
      &lt;routers&gt;
          &lt;adminhtml&gt;
              &lt;use&gt;admin&lt;/use&gt;
              &lt;args&gt;
                  &lt;modules&gt;
                      &lt;WACI_SalesExt after="Mage_Adminhtml_Sales"&gt;WACI_SalesExt_Adminhtml&lt;/WACI_SalesExt&gt;
                  &lt;/modules&gt;
              &lt;/args&gt;
          &lt;/adminhtml&gt;    
      &lt;/routers&gt;
  &lt;/admin&gt;
  &lt;/config&gt;
  </code></pre>
  
  <p><strong>WACI / SalesExt / sql / waci_salesext_setup / mysql4-install-0.2.0.php</strong></p>
  
  <pre><code>    $installer = $this;
      $installer-&gt;startSetup();
  
      $installer-&gt;addAttribute('order', 'dynamics_ord', array(
                                  'type'              =&gt;'varchar', 
                                  'visible'           =&gt; true, 
                                  'required'          =&gt; false, 
                                  'is_user_defined'   =&gt; false, 
                                  'note'              =&gt; 'Dynamics ORD')
                              );
  
  $installer-&gt;getConnection()-&gt;addColumn($installer-&gt;getTable('sales_flat_order'), 'dynamics_ord','VARCHAR(255) NULL DEFAULT NULL');
  $installer-&gt;getConnection()-&gt;addColumn($installer-&gt;getTable('sales_flat_order_grid'), 'dynamics_ord','VARCHAR(255) NULL DEFAULT NULL');
  
      $installer-&gt;endSetup();
  </code></pre>
  
  <p><strong>template/sales/order/view/info.phtml</strong></p>
  
  <pre><code>&lt;?php echo $this-&gt;getLayout()-&gt;createBlock('WACI_SalesExt/adminhtml_sales_dynamicsord')-&gt;setOrderId($_order-&gt;getId())-&gt;toHtml() ?&gt;
  </code></pre>
  
  <p><strong>template/sales/ord_form.phtml</strong></p>
  
  <pre><code>&lt;?php if ('sales_order' == Mage::app()-&gt;getRequest()-&gt;getControllerName() ): ?&gt;
  &lt;tr&gt;
      &lt;td class="label"&gt;&lt;label for="dynamics_ord"&gt;Dynamics ORD&lt;/label&gt;&lt;/td&gt;
      &lt;td class="value"&gt;
          &lt;form action="&lt;?php echo $this-&gt;getFormUrl(); ?&gt;" method="post" id="ord_form"&gt;
              &lt;input type="hidden" name="form_key" value="&lt;?php echo $this-&gt;getFormKey(); ?&gt;" /&gt;
              &lt;input type="hidden" name="token" value="&lt;?php echo $this-&gt;getToken(); ?&gt;" /&gt;  
              &lt;div class=""&gt;
                  &lt;input type="text" class="input-text" name="dynamics_ord" value="&lt;?php echo $this-&gt;getOrder()-&gt;getData('dynamics_ord'); ?&gt;" style="width:150px; float:left; display:inline;" /&gt;
                  &lt;button onclick="editForm.submit()" style="float:left;margin:3px 10px 0;"&gt;Update&lt;/button&gt;  
              &lt;/div&gt; 
          &lt;/form&gt;
          &lt;script type="text/javascript"&gt;
              editForm = new varienForm('ord_form');
          &lt;/script&gt;
      &lt;/td&gt;
  &lt;/tr&gt;
  &lt;?php endif; ?&gt;
  </code></pre>
  
  <p><strong>WACI_SalesExt_Block_Adminhtml_Sales_Dynamicsord</strong></p>
  
  <pre><code>class WACI_SalesExt_Block_Adminhtml_Sales_Dynamicsord extends Mage_Adminhtml_Block_Template
  {
      public function __construct()
      {
          parent::__construct();
          $this-&gt;setTemplate('sales/ord_form.phtml');
      }
  
      public function getOrder(){
          return Mage::registry('current_order');
      }
  
      public function getFormUrl(){
          return Mage::helper("adminhtml")-&gt;getUrl('*/sales_order/editord', array('order_id'=&gt;$this-&gt;getOrder()-&gt;getId()) );
      }
  
  }
  </code></pre>
  
  <p><strong>WACI_SalesExt_Adminhtml_Sales_OrderController</strong></p>
  
  <pre><code>include_once Mage::getModuleDir('controllers', 'Mage_Adminhtml') . DS . 'Sales' . DS . 'OrderController.php';
  
  class WACI_SalesExt_Adminhtml_Sales_OrderController extends Mage_Adminhtml_Sales_OrderController
  {
  
      public function editordAction()
      {
  
          $postData = $this-&gt;getRequest()-&gt;getPost();
  
          Mage::log('WACI_SalesExt_Adminhtml_Sales_OrderController::ordEditAction'.print_r($postData, true) );
  
          $id = $this-&gt;getRequest()-&gt;getParam('order_id');
          $order = Mage::getModel('sales/order')-&gt;load($id);
          $order-&gt;setDynamicsOrd($postData['dynamics_ord']);
  
          $order-&gt;save();
  
          // return to sales_order view
          Mage::app()-&gt;getResponse()-&gt;setRedirect(Mage::helper('adminhtml')-&gt;getUrl("adminhtml/sales_order/view", array('order_id'=&gt; $id)));
  
      }
  
  }
  </code></pre>
  
  <p>Been looking around for some info on this issue, and <a href="https://stackoverflow.com/questions/12936470/adding-a-new-attribute-to-order-in-magento">this SO thread</a> seems pretty close to what I want to do. But it's not clear that I'll be able to create an editable input via this workflow.</p>
  
  <p>I'll note that I'm on Mage 1.11 - and I've gathered that the standard EAV pattern is no longer used for the order model. </p>
  
  <p>Cheers -
  <br><br></p>
  
  <hr>
  
  <h3>Update</h3>
  
  <p>(note the "current" module contents.)</p>
  
  <ul>
  <li>The field has been added to the sales_flat_order <br></li>
  <li>WACI_SalesExt_Block_Adminhtml_Sales_Dynamicsord block is getting constructed correctly <br></li>
  <li>i have some test data in place that is getting pulled in correctly</li>
  </ul>
  
  <p>At this point, I don't think i have my router set up correctly. In your example, you called <code>&lt;?php echo $this-&gt;getEditFormUrl(); ?&gt;</code> . I'm not sure if that is supposed to output a formatted action url, but it wasn't. I dropped my own in, but its still not hitting the router correctly. </p>
  
  <p>Currently, the value in the form action =</p>
  
  <pre><code>   action ="http://my.domain.com/index.php/my_admin/sales_order/editord/key/2e56d7f560dc4d356e3ef9519764b8a84858e5cd40d3c1f5821a423b949b9a6a/"
  </code></pre>
  
  <p>And just hits a "page not found". As far as I can tell, the router <em>should</em> be accurate here. I've tried a number of different combinations; I'm apparently missing something important on the admin router issue. I understand the <code>key/ee6</code>... is probably messing with me, but I'm not clear how to deal with it appropriately.</p>
  
  <hr>
  
  <h3>Update</h3>
  
  <p>Routing problem was simple error I was needlessly making complex: I had a miss-spelled directory name "AdminHtml" vs "Adminhtml" - causing the router to get lost.</p>
  
  <p>Oh Magento...</p>
  
  <p>Thanks @R.S. for teh help.</p>