Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add custom field to order in Magento?

Tags:

php

xml

magento

I want to add one new custom field to one page checkout in Magento. I have created a module with installer:

$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$setup->addAttribute('order', 'deliverydate', array(
    'position'      => 1,
    'input'         => 'text',
    'type'          => 'varchar',
    'label'         => 'Choose delivery date',
    'visible'       => 1,
    'required'      => 0,
    'user_defined' => 1,
    'global'        => 1,
    'visible_on_front'  => 1,
));

$installer->endSetup();

I can see with phpmyadmin that the field has been added to mage_eav_attribute table. I have also added this to shipping-method.phtml :

<div class="form-list field">
   <label for="deliverydate"><?php echo $this->__('Choose delivery date') ?></label>
       <div class="input-box">
      <input type="text" name="deliverydate" id="deliverydate" title="<?php echo $this->__('deliverydate') ?>" class="input-text" />
   </div>
</div>

When I place an order, the custom field won't save. How to make it work? I have added some custom fields to customer with this way and they are working fine. I can also see my custom customer fields automatically on admin/customer but can't see my custom order field in admin/sales_order. Thank you

*Edit:

This is what I have done:

I added observer.php to companyname/module/model/observer.php

class Company_Module_Model_Observer
{
    public function Deliverydate($observer){
        //get event data
        $event = $observer->getEvent();

                //get order
        $order = $event->getOrder();

        //set the country here
        $order->setDeliverydate('11.11.2012');
        //echo "observer";
        }   
}

And here's my config.xml:

<?xml version="1.0"?>
<config>
    <modules>
        <Company_Module>
            <version>0.1.0</version>
        </Company_Module>
    </modules>

    <global>
        <resources>
            <Company_Module_setup>
                <setup>
                    <module>Company_Module</module>
                    <class>Company_Module_Model_Resource_Mysql4_Setup</class>
                </setup>
            </Company_Module_setup>
        </resources>

        <events>
            <checkout_type_onepage_save_order>
                <observers>
                    <Company_Module_Model_Observer>
                        <type>singleton</type>
                        <class>Company_Module_Model_Observer</class>
                        <method>Deliverydate</method>
                    </Company_Module_Model_Observer>
                </observers>
            </checkout_type_onepage_save_order>
        </events>
    </global>
</config>

I think there's some problem with my observer. I think it's not firing.

*Edit:

My observer is working now. The filename must be Observer.php, not observer.php.

like image 889
Joonas Avatar asked Jul 31 '12 09:07

Joonas


People also ask

How do you add a field before shipping methods in checkout page Magento 2?

Magento 2, To display the Input field before shipping methods, you need to create a checkout_index_index. xml file to set the content of the file. 1) Use the “before-form” section of the shippingAddress children in the shipping step hierarchies.


1 Answers

Add an observer :

<events>
    <checkout_type_onepage_save_order>
        <observers>
            <Savefield_observer>
                <type>singleton</type>
                <class>mymodule/observer</class>
                <method>Savefield</method>
            </Savefield_observer>
        </observers>
    </checkout_type_onepage_save_order>
</events>

And in the mymodule/savefield/model/obsever.php file add this:

class Mymodule_Savefield_Model_Observer
{
    public function Savefield($observer){
        //get event data
        $event = $observer->getEvent();

        //get order
        $order = $event->getOrder();

        //set the variable here
        $order->setMyField($fieldVal);
    }   
}
like image 69
Gershon Herczeg Avatar answered Sep 22 '22 19:09

Gershon Herczeg