0

I am developing an custom extension where i have to save two custom attributes when creating a new order. I have two custom inputs where you place the data.

While i am submitting data with the first input there is no problems, the data is saved and after that i can print it the admin order view page.

Here is my config file:

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

    <global> 
         <fieldsets>
            <sales_convert_quote>                           
                <my_custom_input_field_one><to_order>*</to_order></my_custom_input_field_one>
                <my_custom_input_field_two><to_order>*</to_order></my_custom_input_field_two>
            </sales_convert_quote>

            <sales_convert_order>                                              
                <my_custom_input_field_one><to_quote>*</to_quote></my_custom_input_field_one>
                <my_custom_input_field_two><to_quote>*</to_quote></my_custom_input_field_two>
            </sales_convert_order>
        </fieldsets>
        <helpers>
            <vshippingone>
                <class>VivasIndustries_Vshippingone_Helper</class>
            </vshippingone>            
        </helpers>

        <models>
            <vshippingone>
                <class>VivasIndustries_Vshippingone_Model</class>
                <resourceModel>vshippingone_mysql4</resourceModel>
            </vshippingone>
        </models>
        <resources>
            <vshippingone_setup>
                <setup>
                    <module>VivasIndustries_Vshippingone</module>
                    <class>Mage_Sales_Model_Mysql4_Setup</class>
                </setup>
                <connection>
                    <use>core_setup</use>
                </connection>
            </vshippingone_setup>
            <vshippingone_write>
                <connection>
                    <use>core_write</use>
                </connection>
            </vshippingone_write>
            <vshippingone_read>
                <connection>
                    <use>core_read</use>
                </connection>
            </vshippingone_read>
        </resources>

        <events>
            <checkout_controller_onepage_save_shipping_method>
                 <observers>
                     <vshippingone>
                          <type>singleton</type>
                          <class>vshippingone/observer</class>
                          <method>saveCustomDataOne</method>
                     </vshippingone>
                 </observers>
            </checkout_controller_onepage_save_shipping_method>
            <checkout_controller_onepage_save_shipping_method>
                 <observers>
                     <vshippingone>
                          <type>singleton</type>
                          <class>vshippingone/observer</class>
                          <method>saveCustomDataTwo</method>
                     </vshippingone>
                 </observers>
            </checkout_controller_onepage_save_shipping_method>         
        </events>

        <blocks>
            <vshippingone>
                <class>VivasIndustries_Vshippingone_Block</class>
            </vshippingone>
        </blocks>
    </global>
</config>

Here is my Observer:

<?PHP
class VivasIndustries_Vshippingone_Model_Observer 
{
    public function saveCustomDataOne($event)
    {
        $quote = $event->getQuote();
        $quote->setData('my_custom_input_field_one', $event->getRequest()->getPost('shipping_address_id'));

        return $this;
    }
    public function saveCustomDataTwo($event)
    {
        $quote = $event->getQuote();
        $quote->setData('my_custom_input_field_two', $event->getRequest()->getPost('shipping_office'));

        return $this;
    }   
}

Somehow i am making something wrong with the second custom field. The data is not saved to the database. Where i my mistake and how can i make it save the data ?

Thanks in advance!

1

The reason your example above is not working is because you need to rename one of the element name vshippingone because when magento merge the xmls it become like a rewrite, therefore it will ignore the first one.

    <events>
        <checkout_controller_onepage_save_shipping_method>
             <observers>
                 <vshippingone>   <-- Can NOT be the same name
                      <type>singleton</type>
                      <class>vshippingone/observer</class>
                      <method>saveCustomDataOne</method>
                 </vshippingone>
             </observers>
        </checkout_controller_onepage_save_shipping_method>
        <checkout_controller_onepage_save_shipping_method>
             <observers>
                 <vshippingone>   <-- Can NOT be the same name
                      <type>singleton</type>
                      <class>vshippingone/observer</class>
                      <method>saveCustomDataTwo</method>
                 </vshippingone>

I think it would be a better idea to merge both function into one to make your code a little more efficient.

    <events>
        <checkout_controller_onepage_save_shipping_method>
             <observers>
                 <vshippingone>
                      <type>singleton</type>
                      <class>vshippingone/observer</class>
                      <method>saveCustomData</method>
                 </vshippingone>
             </observers>
        </checkout_controller_onepage_save_shipping_method>         
    </events>

Then in your observe.php

<?PHP
class VivasIndustries_Vshippingone_Model_Observer 
{
    public function saveCustomData($event)
    {
        $quote = $event->getQuote();
        $quote->setData('my_custom_input_field_one', $event->getRequest()->getPost('shipping_address_id'));
        $quote->setData('my_custom_input_field_two', $event->getRequest()->getPost('shipping_office'));
        return $this;
    } 
}
  • So if i set it to vshippingtwo what else i have to change to make it work ? – Venelin Vasilev Sep 24 '15 at 16:23
  • What other issues are you having after making that change? – Renon Stewart Sep 24 '15 at 16:26
  • I tried that as you mention above but it is only saving data to my_custom_input_field_one. There is no data saved to my_custom_input_field_two . – Venelin Vasilev Sep 24 '15 at 16:41
  • Did you clear cache after adding field_two? – Renon Stewart Sep 24 '15 at 16:42
  • My cache is not enabled at all. Also i've added my_custom_input_field_two by PHPMyAdmin but not with the extension sql installer. – Venelin Vasilev Sep 24 '15 at 16:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.