Changing Shipping Address in Magento Before Order Save
Introduction
In Magento, you can use observers to listen for events like sales_order_save_before and perform actions before an order is saved. This allows you to modify shipping addresses based on specific conditions, such as when a flat rate shipping method is selected. In this post, we’ll guide you through implementing this functionality using a Magento observer.
Understanding the Problem
The original code snippet provided in the question attempts to access and modify the shipping address within the sales_order_save_before observer event. The issue lies in how the shipping address is retrieved and saved.
Retrieving the Shipping Address
To retrieve the shipping address, you should always use the order object’s method $order->getShippingAddress(). This ensures that you’re working with the correct address associated with the current order being processed by the observer.
The code snippet provided initially attempts to retrieve the shipping address using $shipping_address = $order->getShippingAddress();, which is correct. However, in Magento 2, the shipping method should be retrieved from the quote object within the checkout session. Using Mage::getSingleton('checkout/session') -> getQuote() -> getShippingAddress()->getShippingMethod() will provide you with the selected shipping method.
Modifying the Shipping Address
Once you have the shipping address object, you can modify its attributes using methods like setStreet(), setCity(), and setPostcode().
$shipping_address = $order->getShippingAddress();
If the selected shipping method is ‘flatrate_flatrate’, you can update the address as follows:
if($shipping_method == 'flatrate_flatrate') {
$shipping_address
->setStreet('Test Street')
->setCity('Test City')
->setPostcode('91100');
}
Saving the Shipping Address
To persist the changes made to the shipping address, you must call the save() method on the $shipping_address object. Failing to do so will result in the modifications being discarded when the order is actually saved.
$shipping_address->save();
Complete Working Example
Here’s the complete working code that retrieves and modifies the shipping address based on the selected shipping method:
class Mydons_Eventdemo_Model_Observer {
public function sales_order_save_before($observer)
{
$order = $observer -> getOrder();
// Retrieve shipping address from order object
$shipping_address = $order -> getShippingAddress();
$shipping_method = Mage::getSingleton('checkout/session') -> getQuote() -> getShippingAddress() -> getShippingMethod();
if($shipping_method == 'flatrate_flatrate') {
// Update shipping address attributes
$shipping_address
->setStreet('Test Street')
->setCity('Test City')
->setPostcode('91100');
}
// Save the modified shipping address
$shipping_address->save();
}
}
Conclusion
In summary, to change a shipping address in Magento before an order is saved, you can use the sales_order_save_before observer event. Retrieve the shipping address using the order object’s method $order->getShippingAddress(). Check for the selected shipping method and update the relevant attributes of the shipping address.
Remember to always call the save() method on the modified shipping address object to persist your changes. This ensures that the updated information is reflected in the actual order data after it is saved in Magento.