1

I have an external application and I have to update the billing and shipping details of an existing order. The problem is that the billing city doesn't get saved with the rest of the data. I'm using Magento 1.8. The code was inspired by this thread: Change shipping and billing address from order programmatically

$order = Mage::getModel('sales/order')->load($orderId); 
$shippingAddress = Mage::getModel('sales/order_address')->load($order->getShippingAddress()->getId()); 
$billingAddress = Mage::getModel('sales/order_address')->load($order->getBillingAddress()->getId());

$shippingAddress = Mage::getModel('sales/order_address')
->setFirstname("value")
->setLastname("value")
->setCompany("value")
->setStreet("value"))
->setCity("value")
->setRegion("value")
->setPostcode("value")
->setTelephone("value")->save();

$billingAddress = Mage::getModel('sales/order_address')
->setFirstname("value")
->setLastname("value")
->setCompany("value")
->setStreet("value"))
->setCity("value")
->setRegion("value")
->setPostcode("value")
->setTelephone("value")->save();
  • Does it have to do with the fact that I don't add the Region_id value? If so, why does it save the shipping address and the billing address remains unchanged? – Deni Oct 26 '14 at 8:31
0

Deni,you have already loaded shipping Address and Billing address from order So you don't need create new Mage::getModel('sales/order_address') object You just need to edit address object

$shippingAddress = Mage::getModel('sales/order_address')->load($order->getShippingAddress()->getId()); 
$billingAddress = Mage::getModel('sales/order_address')->load($order->getBillingAddress()->getId());

/* here i have edit exiting order shipping objects */
$shippingAddress ->
->setFirstname("value")
->setLastname("value")
->setCompany("value")
->setStreet("value"))
->setCity("value")
->setRegion("value")
->setPostcode("value")
->setTelephone("value")->save();

$billingAddres
->setFirstname("value")
->setLastname("value")
->setCompany("value")
->setStreet("value"))
->setCity("value")
->setRegion("value")
->setPostcode("value")
->setTelephone("value")->save();

You need to add ->setRegionId() for get region if you region id of this country

  • My original code is working just fine, except it doesn't save the $billingAddres ->setCity("value"). Why does it save everything else, but not that value? The shipping address city is saved just fine. Could be a bug in Magento? – Deni Oct 26 '14 at 9:21
  • $billingAddres ->setRegion("value") doesn't save. Sorry for the mistype. Your code is working the region. – Deni Oct 26 '14 at 9:42
  • $billingAddres ->setRegionId() use... – Amit Bera Oct 26 '14 at 9:46
0

Try this in place of your code snippet:

$order = Mage::getModel('sales/order')->load($orderId); 
$orderAddress = Mage::getModel('sales/order_address');
$shippingAddress = $orderAddress->load($order->getShippingAddress()->getId(); 
$billingAddress = $orderAddress->load($order->getBillingAddress()->getId();

$shippingAddress                                     /* = Mage::getModel('sales/order_address') */
->setFirstname("value")
->setLastname("value")
->setCompany("value")
->setStreet("value")
->setCity("value")
->setRegion("value")
->setPostcode("value")
->setTelephone("value")->save();

$billingAddress                                      /* = Mage::getModel('sales/order_address') */
->setFirstname("value")
->setLastname("value")
->setCompany("value")
->setStreet("value")
->setCity("value")
->setRegion("value")
->setPostcode("value")
->setTelephone("value")->save();
  • I get: Fatal error: Call to a member function getId() on a non-object on this line: $shippingAddress = $orderAddress->load($order->getShippingAddress()->getId()); Also there's double )) on setStreet in your code. – Deni Oct 26 '14 at 9:09
  • Edited the code! Please try now! – Rahul Kumar Das Oct 26 '14 at 9:26
  • My original code is working, except the Region doesn't save. – Deni Oct 26 '14 at 9:44
0

Here is the complete working code, I also included a custom shipping and billing attribute in case someone needs it:

Mage::app('default');
$order = Mage::getModel('sales/order')->loadByIncrementId($_POST['nr_comanda']);
$shippingAddress = Mage::getModel('sales/order_address')->load($order->getShippingAddress()->getId()); 
$billingAddress = Mage::getModel('sales/order_address')->load($order->getBillingAddress()->getId());

$shippingAddress
->setCompany($_POST[''])    
->setFirstname($_POST[''])
->setLastname($_POST[''])
->setTelephone($_POST[''])
->setStreet($_POST[''])
->setCity($_POST[''])
->setRegion($_POST[''])
->setData('custom_attribute', '123')
->setPostcode($_POST[''])->save();

$billingAddress
->setCompany($_POST[''])    
->setFirstname($_POST[''])
->setLastname($_POST[''])
->setTelephone($_POST[''])  
->setStreet($_POST[''])
->setCity($_POST[''])
->setRegion($_POST[''])
->setData('custom_attribute', '123')
->setPostcode($_POST[''])->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.