0

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.

2
  • Have you added new_address_code field to the sales_flat_order_address table?
    – mageUz
    Aug 8 '14 at 13:09
  • @mageUz through mysql-install and the config file Aug 8 '14 at 13:49
1

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

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.