Magento – Add custom field to order in admin backend and allow to change it

backendeavextensionsmagento-1.9

I want to add a custom checkbox to all orders that is by default deactivated, allow the admins to change the value of this field in the backend and filter the grid by this!

What I have so far is:

Module config:

<?xml version="1.0"?>
<config>
    <modules>
        <demo_OrderCheckbox>
            <version>1.0.0</version>
        </demo_OrderCheckbox>
    </modules>
    <global>
        <resources>
            <Ordercheckbox_setup>
                <setup>
                    <module>demo_OrderCheckbox</module>
                    <class>demo_OrderCheckbox_Model_Resource_Setup</class>
                </setup>
            </Ordercheckbox_setup>
        </resources>
    </global>
    <adminhtml>
        <layout>
            <updates>
                <demo_OrderCheckbox>
                    <file>demo/ordercheckbox.xml</file>
                </demo_OrderCheckbox>
            </updates>
        </layout>

    </adminhtml>
</config>

The setup file:

<?php
$installer = $this;
$installer->startSetup();
$installer->addAttribute("order", "handled", array("type" => "checkbox", 'label' => 'Order handled', 'visible' => true, 'required' => false, 'is_user_defined' => true,));
$installer->endSetup();

The demo/ordercheckbox.xml:

<?xml version="1.0" ?>
<layout>
    <sales_order_grid_update_handle>
        <reference name="sales_order.grid">
            <action method="addColumnAfter">
                <columnId>handled</columnId>
                <arguments>
                    <header>handled</header>
                    <index>handled</index>
                    <filter_index>handled</filter_index>
                    <type>checkbox</type>
                </arguments>
                <after>shipping_name</after>
            </action>
        </reference>
    </sales_order_grid_update_handle>
    <adminhtml_sales_order_grid>
        <!-- apply layout handle defined above -->
        <update handle="sales_order_grid_update_handle" />
    </adminhtml_sales_order_grid>
    <adminhtml_sales_order_index>
        <!-- apply layout handle defined above -->
        <update handle="sales_order_grid_update_handle" />
    </adminhtml_sales_order_index>
</layout>

In the grid, there is a new field, but it is checked for every order and if I try to filter by it, there is an error a:5:{i:0;s:85:"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'handled' in 'where clause'";i:1;s:6103:"#0 /home/web/htdocs/lib/Varien/Db/Statement/Pdo/Mysql.php(110): Zend_Db_Statement_Pdo->_execute(Array)

How do I get this to work?
Then I need to make it changeable in the detail view.
I added this code to app/design/adminhtml/default/default/template/sales/order/view/form.phtml

<?php
$_order_id = $this->getRequest()->getParam('order_id');
if ($_order_id>0):
$_order = Mage::getModel('sales/order')->loadByIncrementId($_order_id);
$isHandled=$_order->getHandled();
?>
<div class="entry-edit" id="sales_order_view"><div id="handled-status"><span class="bold">Already handled</span>  <input type="checkbox" name="handled"<?php if($isHandled) { echo ' checked '; } ?> value="true" id="handledCheckbox"></div></div>
<script type="text/javascript">
var clickHandler = function() {
  var ch = jQuery(this);
  res = false; //add here ajax call to update status
  ch.prop('checked', res);
}
  jQuery('#handledCheckbox').on('change',clickHandler);
</script>
<?php
endif;

I plan to add a method to change this value to my module and call that method using AJAX!

Should my approach work after I fix the errors/problems or am I completely on the wrong track?

Best Answer

I guess the problem is that you set-up script did not run correctly and so the attribute was not added to the order table.

Please check the following:

  1. core_resource table for an entry Ordercheckbox_setup,
  2. Possibly it does not like the upper case name so try to update it to ordercheckbox_setup and update the folder,
  3. Validate that the folder naming and script naming is correct,
Related Topic