1

I've been trying to add a field to checkout where the customer could add a reference number. Right now it sort of works, but my observer grabs the previous order and adds the information to that order in the database. So what i need to figure out is how do i get it to pick the next order / the one being ordered or get some hints on how to do it in a different way. I'm working with Magento 1.9.0.1

Config.xml

    <events>
        <adminhtml_sales_order_create_process_data_before> <!-- Adding reference number in backend working -->
            <observers>
                <customorderfield_backend>
                    <type>singleton</type>
                    <class>customorderfield/observer</class>
                    <method>saveCustomData</method>
                </customorderfield_backend>
            </observers>
        </adminhtml_sales_order_create_process_data_before>

        <controller_action_predispatch_checkout_onepage_saveBilling> <!-- Adding reference number on onepage doesn't work -->
            <observers>
                <customorderfield_onepage>
                    <type>singleton</type>
                    <class>customorderfield/observer</class>
                    <method>saveCustomDataOnepage</method>
                </customorderfield_onepage>
            </observers>
        </controller_action_predispatch_checkout_onepage_saveBilling>
    </events>

Observer.php

    class Lightsolutions_CustomOrderField_Model_Observer 
    {
       public function saveCustomData($event) //Adding reference number in backend working
    {
       $quote = $event->getSession()->getQuote();
       $quote->setData('my_custom_input_field', $event->getRequestModel()->getPost('my_custom_input_field'));

       return $this;
    }

    public function saveCustomDataOnepage($observer) //Adding reference number on onepage doesn't work
    {
        $orders = Mage::getModel('sales/order')->getCollection()
         ->setOrder('created_at','DESC')
         ->setPageSize(1)
         ->setCurPage(1);
        $orderId = $orders->getFirstItem()->getEntityId();
        $order = Mage::getModel('sales/order')->load($orderId);
        $order->setData('my_custom_input_field', $_POST['my_custom_input_field']);
        $order->save();
    }

mysql4-install-1.1php

$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();
  • You never want to load a collection and an order in an observer, without the direct reference to it, as you do in saveCustomDataOnepage. Since you're using an observer, use the observer class to obtain your quote, or you can even use your customers' session (Mage::getSingleton('checkout/session')), but you don't want to load your own collection. – Maikel Koek Mar 29 '17 at 9:53
0

Thank you @Maikel Koek for your input.

I've modified my code and now it works with our 3rd party checkout. The main difference with our 3rd party checkout system and the standard Magento Onepage is that the standard version submits the data when moving on to the next step. Where as i am able to get the $_POST value from the billing address in the 3rd party extension without any problems.

My code now is as following in case someone stumbles upon this post.

Config.Xml

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

        <checkout_type_onepage_save_order_after>
            <observers>
                <customorderfield_onepage>
                    <class>customorderfield/observer</class>
                    <method>saveCustomDataOnepage</method>
                </customorderfield_onepage>
            </observers>
        </checkout_type_onepage_save_order_after>
    </events>

Observer.php

class Lightsolutions_CustomOrderField_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;
    }

    public function saveCustomDataOnepage($event) {
        $orderId = $event->getOrder()->getId();
        $order = Mage::getModel('sales/order')->load(intval($orderId));
        $order->setData('my_custom_input_field', $_POST['my_custom_input_field']);
        $order->save();
    }
}

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.