At the end of the purchase, the user has the possibility to change your shipping address. Im trying to update this information, but it dosen't work. This is my code:
$customer = Mage::getModel('customer/session')->getCustomer();
$order = Mage::getSingleton('checkout/session')->getLastOrder();
$postData = Mage::app()->getRequest()->getPost();
$_new_address = array (
'firstname' => $postData['nombre'],
'lastname' => $postData['apellidos'],
'street' => array ('0' => $postData['direccion']),
'city' => $postData['localidad'],
'region_id' => $postData['provincia_id'],
'region' => '',
'postcode' => $postData['codigo_postal'],
'country_id'=> 'ES',
'telephone' => $postData['telefono']
);
$customAddress = Mage::getModel('customer/address');
$customAddress->setData($_new_address)
->setCustomerId($customer->getId())
->setIsDefaultBilling('1')
->setIsDefaultShipping('1')
->setSaveInAddressBook('1');
// Save address
try {
$customAddress->save();
} catch (Exception $e) {
Mage::getSingleton('core/session')->addError($e->getMessage());
header('Location: /');
exit;
}
// Update the order
try {
$order->setShippingAddress($customAddress)->save();
} catch (Exception $e) {
Mage::getSingleton('core/session')->addError($e->getMessage());
header('Location: /');
exit;
}
Can I update an order or is not allowed? Can anyone give me a tip?
My problem was that the Billing address and shipping address in the order, are different than having the user has as default addresses.
At the end the code looks like this:
$order = Mage::getSingleton('checkout/session')->getLastOrder();
$postData = Mage::app()->getRequest()->getPost();
// Try to get shipping and billing address data.
$orderShippingAddress = $order->getShippingAddress()->getId();
$orderShipping = Mage::getModel('sales/order_address')->load($orderShippingAddress);
$orderBillingAddress = $order->getBillingAddress()->getId();
$orderBilling = Mage::getModel('sales/order_address')->load($orderBillingAddress);
// Updating data.
$orderShipping->addData($postData);
$orderBilling->addData($postData);
try {
$orderShipping->implodeStreetAddress()->save();
$orderBilling->implodeStreetAddress()->save();
} catch (Exception $e) {
Mage::logException($e);
Mage::getSingleton('core/session')->addError($e->getMessage());
header('Location: /after/success/envio');
exit;
}
Now, it works. Thanks for your help @R.S
Assuming that you want to update the shipping address for an order, take a look @ addressSaveAction()
in /app/code/core/Mage/Adminhtml/controllers/Sales/OrderController.php
$order = Mage::getSingleton('checkout/session')->getLastOrder();
//Get shipping address Id
$addressId = $order->getShippingAddress()->getId();
$address = Mage::getModel('sales/order_address')->load($addressId);
$data = $this->getRequest()->getPost();
$address->addData($data);
$address->implodeStreetAddress()->save();
Also i'm not sure if Mage::getSingleton('checkout/session')->getLastOrder()
will still contain a valid order id after you submit your form since you will be loading a new page
While functioning correctly both other answers have an excessive address loading. The getShippingAddress
already returns instance of sales/order_address
. So the procedure can be simplified to following:
$order = Mage::getSingleton('checkout/session')->getLastOrder();
$data = $this->getRequest()->getPost();
$order->getShippingAddress()->addData($data)
->save();
Also implodeStreetAddress
method call only required if you use multiline street address.