Question

I have a custom field(new_address_code) for customer address in backend. A observer is used to assign a auto-generated value to the field when ever a new address is added through myaddress or in checkout page

<customer_address_save_after>
      <observers>
         <myautoincrement>
            <class>samp/observer</class>
            <method>saveadrsmethod</method>
         </myautoincrement>
      </observers>
</customer_address_save_after>

In the observer class

$customerAddress = $observer->getCustomerAddress();
$id= (string)$customerAddress->getId();
$ObcustomerAddress = $observer->getCustomerAddress();
if ($customerAddress->getId()) {
            $customerAddress->setData('new_address_code','aftertesting-'.$customerAddress->getData('firstname').'=myid'.$id);
             $customerAddress->getResource()->saveAttribute($ObcustomerAddress, 'new_address_code');
   }

Whats the problem here is when a new address is added while checkout, the auto-generated custom fields value is not saved into sales_flat_quote_address table. So, I tried writing an observer for that as follows

<sales_order_place_before>
    <observers>
       <myautoincrement>
          <class>samp/observer</class>
          <method>beforeorder</method>
       </myautoincrement>
    </observers>
</sales_order_place_before>

And in the observer class

public function beforeorder($observer)
{
    $order = $observer->getEvent()->getOrder();
    $customerAddress = $observer->getCustomerAddress();
    if ($customerAddress->getId()) {
        $address=Mage::getModel('sales/order_address')->setData('new_address_code',$customerAddress->getData('new_address_code'));
        $order->setBillingAddress($address);
        $order->setShippingAddress($address);
    }
}

But it's not saving in the sales_flat_order_address table. Could anybody tell me what could be the reason.

Was it helpful?

Solution

This line doesn't seem correct because $address will be a new empty object (except for new_address_code value) therefore $order->setBillingAddress() will be empty

$address=Mage::getModel('sales/order_address')->setData('new_address_code',$customerAddress->getData('new_address_code'));

$order->setBillingAddress($address);
$order->setShippingAddress($address);

Take a look at programmatically create order in magento

Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top