Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8125731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:54:40+00:00 2026-06-06T06:54:40+00:00

I am trying to create or update default shipping address for a customer using

  • 0

I am trying to create or update default shipping address for a customer using customer_save_after event (based on some custom registration fields) when customer tries to create new account or edit his account.

Here goes the portion of the Observer model code:

...
$customer   = $observer->getEvent()->getCustomer();
if ($customer->getId() && $otherConditionIsValid){
    $dataShipping = array(
        'firstname' => $someFixedFirstName,
        'lastname'  => $someFixedLastName,
        'street'    => array($someFixedStreetLine),
        'city'      => $someFixedCity,
        'region'    => $someFixedState,
        'region_id' => '',
        'postcode'  => $someFixedZipcode,
        'country_id' => $someFixedCountry,
        'telephone' => $someFixedTelephone,
    );
    $customerAddress = Mage::getModel('customer/address');
    if($defaultShippingId = $customer->getDefaultShipping()){ //if customer already has default shipping address
        $customerAddress->load($defaultShippingId);
    }
    $customerAddress->setData($dataShipping)
                    ->setCustomerId($customer->getId())
                    ->setIsDefaultShipping('1')
                    ->setSaveInAddressBook('1');
    $customer->addAddress($customerAddress); #setting Shipping Address to be added/updated
    //When i try to use below commented code, script gets called for infinite times, finally running out of memory
    /*try{
        $customerAddress->save();
    }catch(Exception $e){
        Mage::log('Address Save Error::' . $e->getMessage());
    }*/
}
...

Above code works fine when customer creates a new account. However default shipping address don’t get updated when customer tries to edit the custom registration field from My Account > Account Information

So my main concern is how to update the shipping address using $customer object or any other code using customer_save_after event.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T06:54:43+00:00Added an answer on June 6, 2026 at 6:54 am

    If I understand correctly, you would like to do the following when the customer_save_after event gets fired:

    1. If the customer does not have a default shipping address, then create one with the values in the $dataShipping array.

    2. If the customer does have a default shipping address, then update it’s values with those in the $dataShipping array.

    If this is indeed what you are looking for then please try the code below (obviously you need to take care of where the values for the $otherConditionIsValid and $dataShipping variables):

    $customer = $observer->getEvent()->getCustomer();
    if (! $customer->getId() || ! $otherConditionIsValid){
        return $this;                
    }
    
    $dataShipping = array(
        'firstname'  => $someFixedFirstName,
        'lastname'   => $someFixedLastName,
        'street'     => array($someFixedStreetLine),
        'city'       => $someFixedCity,
        'region'     => $someFixedState,
        'region_id'  => '',
        'postcode'   => $someFixedZipcode,
        'country_id' => $someFixedCountry,
        'telephone'  => $someFixedTelephone,
    );
    
    $customerAddress = Mage::getModel('customer/address');
    
    if ($defaultShippingId = $customer->getDefaultShipping()){
         $customerAddress->load($defaultShippingId); 
    } else {   
         $customerAddress
            ->setCustomerId($customer->getId())
            ->setIsDefaultShipping('1')
            ->setSaveInAddressBook('1')
         ;   
    
         $customer->addAddress($customerAddress);
    }            
    
    try {
        $customerAddress
            ->addData($dataShipping)
            ->save()
        ;           
    } catch(Exception $e){
        Mage::log('Address Save Error::' . $e->getMessage());
    }
    
    return $this; 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to block all default methods except create and update in my
I am trying to create an event and store it in the default calendar
I'm trying to create/update a location while saving a checkin in my Rails project,
I am trying to create Update trigger which should be invoked only if the
I'm trying to a create an update statement along the following lines: TABLE car:
I am trying to create a trigger to update another table with a condition
Hi i'm trying to create and distribute an update/patch for my app on app
I'm trying to create an SQL query in PHP to update a table. Is
I am trying to update a table according to this trigger: CREATE TRIGGER alert
I am trying to create a simple portlet for Liferay 5.2.2 using Grails 1.2.1

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.