Magento - How to update shipping address using customer_save_after event?

Magento - How to update shipping address using customer_save_after event?

September 11, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento - How to update shipping address using customer_save_after event?

Updating Shipping Address in Magento: A Practical Guide

Introduction

In this tutorial, we’ll guide you through updating or creating a customer’s default shipping address using the customer_save_after event in Magento. This is particularly useful when you need to automatically set or update specific shipping details based on custom fields during customer registration or profile editing.

The Problem: Infinite Loop and Memory Exhaustion

The provided code attempts to save changes to the shipping address using a try-catch block within an observer for the customer_save_after event. However, when attempting to use $customerAddress->save(), the script enters an infinite loop and eventually runs out of memory.

This issue arises because saving an address triggers another customer_address_save_after event, which then triggers this original observer again, creating a recursive loop. Additionally, using addAddress() without explicitly setting an ID can cause Magento to create a new address entry instead of updating the existing one.

The Solution: Correct Address Update Procedure

Step 1: Load the Existing Shipping Address

First, ensure you are always working with the correct shipping address by loading it using the customer’s default shipping address ID:

if ($defaultShippingId = $customer->getDefaultShipping()) {
    $customerAddress = Mage::getModel('customer/address')->load($defaultShippingId);
} else {
    // If no default address, create a new one
    $customerAddress = Mage::getModel('customer/address');
}

Step 2: Set the Address Data and Customer Association

Set all the required data for the shipping address and associate it with the customer:

$customerAddress->setData($dataShipping)
                ->setCustomerId($customer->getId())
                ->setIsDefaultShipping('1')
                ->setSaveInAddressBook('1');

Step 3: Update the Customer’s Address Collection

Instead of trying to save the address directly, which triggers events recursively, you can update the customer’s addresses collection:

$customer->unsAddresses();
$customer->getAddressesCollection()->addItem($customerAddress);
$customer->save();

Step 4: Implementing the Observer

Putting it all together, your observer model should look like this:

public function saveCustomerShippingAddress(Varien_Event_Observer $observer)
{
    $customer = $observer->getEvent()->getCustomer();

    if ($customer->getId() && $otherConditionIsValid) {
        // Load the shipping address or create a new one
        if ($defaultShippingId = $customer->getDefaultShipping()) {
            $customerAddress = Mage::getModel('customer/address')->load($defaultShippingId);
        } else {
            $customerAddress = Mage::getModel('customer/address');
        }

        // Set the address data and customer association
        $customerAddress->setData($dataShipping)
                        ->setCustomerId($customer->getId())
                        ->setIsDefaultShipping('1')
                        ->setSaveInAddressBook('1');

        // Update the customer's addresses collection
        $customer->unsAddresses();
        $customer->getAddressesCollection()->addItem($customerAddress);
        $customer->save();
    }
}

Conclusion

Updating a customer’s default shipping address in Magento requires careful handling of events to avoid infinite loops and memory exhaustion. By loading the existing address (or creating a new one if necessary), setting the required data, and updating the customer’s addresses collection directly, you can ensure that the shipping address is correctly updated without triggering recursive event calls.

Remember to set up your observer model correctly in the config.xml file under the <global> or <frontend> nodes:

<global>
    <events>
        <customer_save_after>
            <observers>
                <your_module_customer_save_after_observer>
                    <type>singleton</type>
                    <class>Your_Module_Model_Observer</class>
                    <method>saveCustomerShippingAddress</method>
                </your_module_customer_save_after_observer>
            </observers>
        </customer_save_after>
    </events>
</global>

For more advanced customizations and Magento development best practices, consider exploring the resources available at Laravel Company. They provide comprehensive guides on Magento 2 development, performance optimization, and extension creation.

By following this approach, you’ll be able to automatically update or create default shipping addresses for customers based on custom registration fields without encountering the infinite loop issue.

Extensions Configuration Customers Errors

Our network