2

I am trying to add a custom field containing the deliverydate to the checkout of Magento. However I have problems saving it. I have added the field to the shipping_method page and set up an observer in the config.xml:

<frontend>
    <events>
        <controller_action_predispatch_checkout_onepage_saveShippingMethod>
            <observers>
                <mymodule_deliverydate>
                    <type>model</type>
                    <class>mymodule_deliverydate/observer</class>
                    <method>saveDeliverydate</method>
                </mymodule_deliverydate>
            </observers>
        </controller_action_predispatch_checkout_onepage_saveShippingMethod>
    </events>

</frontend>

Also, the quote to order conversion is set up in the config.xml:

<global>
....
<fieldsets>
            <sales_convert_quote>
                <deliverydate>
                    <to_order>*</to_order>
                </deliverydate>
            </sales_convert_quote>
        </fieldsets>
</global>

I have added a field deliverydate to the sales_flat_order table ( and sales_flat_quote but Im not too sure if that was necessary) and set up the observer function like this:

public function saveDeliverydate($observer)
    {

    $order = $observer->getEvent()->getOrder();

    $deliverydate = Mage::app()->getRequest()->getPost('deliverydate');

    if(!empty($deliverydate))
    {
        $order->setDeliverydate($deliverydate);
    }
}

However when the order has been placed, the field is empty in the database. What am I missing?

1 Answer 1

2

MAY THIS ASNWER WILL HELP YOU TO SOLVE YOUR ISSUE

CHANGE YOU EVENTS TO:

  <events>
            <checkout_controller_onepage_save_shipping_method>
                <observers>
                    <deliverydate_observer>
                        <type>singleton</type>
                        <class>deliverydate/observer</class>
                        <method>checkout_controller_onepage_save_shipping_method</method>
                    </deliverydate_observer>
                </observers>
            </checkout_controller_onepage_save_shipping_method>
            <sales_order_save_after>
                <observers>
                    <order_set_shippingarrivaldate>
                        <type>model</type>
                        <class>deliverydate/observer</class>
                        <method>saveShippingArrivalDate</method>
                    </order_set_shippingarrivaldate>
                </observers>
            </sales_order_save_after>
        </events>

THEN IN OBSERVER:

public function checkout_controller_onepage_save_shipping_method($observer)
        {
                $request = $observer->getEvent()->getRequest();
                $quote =  $observer->getEvent()->getQuote();

                $desiredArrivalDate = $request->getPost('deliverydate', '');
                if (isset($desiredArrivalDate) && !empty($desiredArrivalDate)){
                    $quote->setdeliverydate($desiredArrivalDate);

                    $quote->save();
                }


            return $this;
        }
 public function saveShippingArrivalDate($observer){

            $order = $observer->getEvent()->getOrder();
            $desiredArrivalDate = Mage::app()->getRequest()->getParam('deliverydate');
            if (isset($desiredArrivalDate) && !empty($desiredArrivalDate)){

                $order->setdeliverydate($desiredArrivalDate);

        }

IF ABOVE saveShippingArrivalDate DOESNT WORK THEN YOU USE BELOW CODE FOR THAT FUNCTION

 public function saveShippingArrivalDate($observer){  $order = $observer->getEvent()->getOrder();
$cart = Mage::getModel('checkout/cart')->getQuote()->getData();
                $desiredArrivalDate =$cart['deliverydate'];

                if (isset($desiredArrivalDate) && !empty($desiredArrivalDate)){

                    $order->setdeliverydate($desiredArrivalDate);
                }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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