Rohit Goel
Rohit Goel

Reputation: 3554

Save custom field value in new order attribute Magento

  1. In this I need to save some custom field value in my order data. For this I have create a small module whose structure is below. This has create a new field in the sales_flat_order table. The observer is also working but when I try to print the custom field value in the observer it shows nothing. So can you please help me that how can I get the value of the custom field in billing form in observer so that I can save that with order data.

app/code/local/Iclp/Orderattribute/etc/config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Iclp_Orderattribute>
      <version>0.1.0</version>
    </Iclp_Orderattribute>
  </modules>
  <global>
    <helpers>
      <orderattribute>
        <class>Iclp_Orderattribute_Helper</class>
      </orderattribute>
    </helpers>
    <models>
      <orderattribute>
        <class>Iclp_Orderattribute_Model</class>
        <resourceModel>orderattribute_mysql4</resourceModel>
      </orderattribute>
    </models>
    <resources>
      <salesattribute1422420996_setup>
        <setup>
          <module>Iclp_Orderattribute</module>
          <class>Mage_Sales_Model_Mysql4_Setup</class>
        </setup>
        <connection>
          <use>core_setup</use>
        </connection>
      </salesattribute1422420996_setup>
      <salesattribute1422420996_write>
        <connection>
          <use>core_write</use>
        </connection>
      </salesattribute1422420996_write>
      <salesattribute1422420996_read>
        <connection>
          <use>core_read</use>
        </connection>
      </salesattribute1422420996_read>
    </resources>
    <events>
      <checkout_type_onepage_save_order> <!-- identifier of the event we want to catch -->
        <observers>
          <checkout_type_onepage_save_order_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>orderattribute/observer</class> <!-- observers class alias -->
            <method>getorderattribute</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </checkout_type_onepage_save_order_handler>
        </observers>
      </checkout_type_onepage_save_order>
    </events>
  </global>
</config> 

app/code/local/Iclp/Orderattribute/Model/Observer.php

<?php
class Iclp_Orderattribute_Model_Observer
{

            public function getorderattribute(Varien_Event_Observer $observer)
            {
        $event = $observer->getEvent();
        $order = $event->getOrder();
        $fieldVal = Mage::app()->getFrontController()->getRequest()->getParams();

        $order->setOrder_Attribute($fieldVal['order_attribute']);
       // echo "success" ;
            }

}

app/code/local/Iclp/Orderattribute/sql/salesattribute1422420996_setup/mysql4-install-0.1.0.php

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

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

template/persistent/checkout/onepage/billing.phtml

<form id="co-billing-form" action="">

<input id = "order_attribute" name="order_attribute" value= " order_attribute order_attribute " class="input-text" type=”text”>

Upvotes: 1

Views: 1891

Answers (1)

Mihir Bhende
Mihir Bhende

Reputation: 9045

Is your custom attribute getting saved in database with the values you assign?

If yes, then in your observer, load the order with order ID or Increment Id and then try to get your custom Attribute.

Upvotes: 1

Related Questions