Magento custom "order" attribute / admin input and display

Magento custom "order" attribute / admin input and display

July 9, 2026 ยท Illustrated author avatarBy Stefan Bogdanescu
Magento custom "order" attribute / admin input and display

Creating a Custom "Order" Attribute for Magento

In this guide, we will walk you through the process of adding a custom attribute to the order model in Magento. We'll create an attribute called "dynamics_ord", which can be defined by admin staff after the order has been saved, ideally on the admin/sales_order/view page within the main order block.

Adding the Custom Attribute

The first step is to add the custom attribute to the sales_flat_order table. You can achieve this by using an SQL script in your module's sql/waci_salesext_setup/mysql4-install-0.2.0.php file:

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

    $installer->addAttribute('order', 'dynamics_ord', array(
                                'type'              =>'varchar',
                                'visible'           => true,
                                'required'          => false,
                                'is_user_defined'   => false,
                                'note'              => 'Dynamics ORD')
                            );

$installer->getConnection()->addColumn($installer->getTable('sales_flat_order'), 'dynamics_ord','VARCHAR(255) NULL DEFAULT NULL');
$installer->getConnection()->addColumn($installer->getTable('sales_flat_order_grid'), 'dynamics_ord','VARCHAR(255) NULL DEFAULT NULL');

    $installer->endSetup();

This script adds the dynamics_ord attribute to both the sales_flat_order and sales_flat_order_grid tables.

Creating the Edit Form in the Order View Page

Next, we need to create an edit form for the new attribute on the order view page (admin/sales_order/view). To do this, we'll modify the template/sales/ord_form.phtml file:

<?php if ('sales_order' == Mage::app()->getRequest()->getControllerName() ): ?>
<tr>
    <td class="label"><label for="dynamics_ord">Dynamics ORD</label></td>
    <td class="value">
        <form action="<?php echo $this->getFormUrl(); ?>" method="post" id="ord_form">
            <input type="hidden" name="form_key" value="<?php echo $this->getFormKey(); ?>" />
            <input type="hidden" name="token" value="<?php echo $this->getToken(); ?>" />
            <div class="">
                <input type="text" class="input-text" name="dynamics_ord" value="<?php echo $this->getOrder()->getData('dynamics_ord'); ?>" style="width:150px; float:left; display:inline;" />
                <button onclick="editForm.submit()" style="float:left;margin:3px 10px 0;">Update</button>
            </div>
        </form>
        <script type="text/javascript">
            editForm = new varienForm('ord_form');
        </script>
    </td>
</tr>
<?php endif; ?>

This snippet adds a text input field for the dynamics_ord attribute and an update button to submit the form. The form is submitted using the editord action, which we'll create next.

Creating the "Edit Order" Action

To handle the form submission, we need to create a new controller method called editordAction in your module's controller file (controllers/Adminhtml/Sales/OrderController.php). This method will update the order with the new attribute value:

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->getRequest()->getPost();

        Mage::log('WACI_SalesExt_Adminhtml_Sales_OrderController::ordEditAction'.print_r($postData, true) );

        $id = $this->getRequest()->getParam('order_id');
        $order = Mage::getModel('sales/order')->load($id);
        $order->setDynamicsOrd($postData['dynamics_ord']);

        $order->save();

        // return to sales_order view
        Mage::app()->getResponse()->setRedirect(Mage::helper('adminhtml')->getUrl("adminhtml/sales_order/view", array('order_id' => $id)));

    }

}

This method retrieves the order ID from the request, loads the corresponding order object, sets the dynamics_ord attribute value using the posted data, and saves the updated order. Finally, it redirects the user back to the sales_order view page for the updated order.

Conclusion

In this guide, we have demonstrated how to create a custom "order" attribute in Magento, including adding the attribute to the database, creating an edit form on the order view page, and handling the form submission using a new controller method. By following these steps, you should be able to add any additional attributes you need for your specific use case.

If you have any further questions or encounter any issues during this process, please don't hesitate to reach out for assistance from our experienced development team at Laravel Company.

Database Extensions Admin Orders

Our network