11

I want to add an attribute to order that will not be visible to customer. I just want to use it in database and storing a specific value for each order. I want to print order according to this value. So how can i add an order attribute in magento. The attribute is just like status of order. Further on if i want to show that attribute in admin/sales/orders how can i do that?

MJQ
  • 1,778
  • 6
  • 34
  • 60

1 Answers1

18

Assuming that you want to add my_custom_input_field to your order and add a field to your admin create order page (to add the field to the frontend you just need to add the input field to the front template and double check the observer)

In /app/code/local/MageIgniter/CustomOrderStatus/etc/config.xml

<?xml version="1.0"?>
<config>
    <modules>
        <MageIgniter_CustomOrderStatus>
            <version>1.1</version>
        </MageIgniter_CustomOrderStatus>
    </modules>

    <global> 
         <fieldsets>
            <sales_convert_quote>                           
                <my_custom_input_field><to_order>*</to_order></my_custom_input_field>
            </sales_convert_quote>

            <sales_convert_order>                                              
                <my_custom_input_field><to_quote>*</to_quote></my_custom_input_field>
            </sales_convert_order>
        </fieldsets>
        <helpers>
            <customorderstatus>
                <class>MageIgniter_CustomOrderStatus_Helper</class>
            </customorderstatus>            
        </helpers>

        <models>
            <customorderstatus>
                <class>MageIgniter_CustomOrderStatus_Model</class>
                <resourceModel>customorderstatus_mysql4</resourceModel>
            </customorderstatus>
        </models>
        <resources>
            <customorderstatus_setup>
                <setup>
                    <module>MageIgniter_CustomOrderStatus</module>
                    <class>Mage_Sales_Model_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </customorderstatus_setup>
            <customorderstatus_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </customorderstatus_write>
            <customorderstatus_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </customorderstatus_read>
        </resources>

        <events>
            <adminhtml_sales_order_create_process_data_before>
                <observers>
                    <customorderstatus>
                        <type>singleton</type>
                        <class>customorderstatus/observer</class>
                        <method>saveCustomData</method>
                    </customorderstatus>
                </observers>
            </adminhtml_sales_order_create_process_data_before>
        </events>

        <blocks>
            <customorderstatus>
                <class>MageIgniter_CustomOrderStatus_Block</class>
            </customorderstatus>
        </blocks>
    </global>
</config>

In /app/code/local/MageIgniter/CustomOrderStatus/sql/customorderstatus_setup/mysql4-install-1.1.php

<?php
$installer = $this;
$installer->startSetup();

$installer->addAttribute("order", "my_custom_input_field", array("type"=>"varchar"));
$installer->addAttribute("quote", "my_custom_input_field", array("type"=>"varchar"));
$installer->endSetup();

In /app/code/local/MageIgniter/CustomOrderStatus/Model/Observer.php

class MageIgniter_CustomOrderStatus_Model_Observer 
{
    public function saveCustomData($event)
    {
        $quote = $event->getSession()->getQuote();
        $quote->setData('my_custom_input_field', $event->getRequestModel()->getPost('my_custom_input_field'));

        return $this;
    }
}

(You should avoid make changes to core default - you should do some research on way around this)

In /app/design/adminhtml/default/default/template/sales/order/view/info.phtml

<?php if($_order->getMyCustomInputField()): ?>
<tr>
    <td class="label"><label><?php echo Mage::helper('sales')->__('My Custom Input Field') ?></label></td>
    <td class="value"><strong><?php echo $_order->getMyCustomInputField() ?></strong></td>
</tr>
<?php endif; ?>

In /app/design/adminhtml/default/default/template/sales/order/create/form/account.phtml

(to add to frontend change the value="<?php echo Mage...>")

<input id="my_custom_input_field" name="my_custom_input_field" value="<?php echo Mage::getSingleton('adminhtml/session_quote')->getQuote()->getMyCustomInputField() ?>" class="input-text" type="text">
MagePal Extensions
  • 17,646
  • 2
  • 47
  • 62
  • 1
    Thanks for sharing a code. I would like to mention a extension here for less technical people to add extra checkout fields with single click from back office. http://www.fmeextensions.com/magento-additional-checkout-attributes-fields.html –  Jan 19 '15 at 07:00
  • How do you add/write to the custom column to the table sales_flat_order_grid also? – user2963379 May 19 '17 at 15:40
  • @user2963379 ... Magento will automatically update if for you on updating of the column with the same name in sales_flat_order – MagePal Extensions May 19 '17 at 15:44
  • I mean I want to add the 'my_custom_input_field' column into the sales_flat_order_grid table. I tried adding to the sql file with an extra addAttribute to "order_grid" but it doesn't seem to work. – user2963379 May 19 '17 at 15:50
  • 1
    @RenonStewart : error : https://snag.gy/aN17pb.jpg code : https://snag.gy/3aSsXn.jpg – Gem Jul 12 '17 at 07:54