0

I have an observer set up for customer login, and it is firing exactly as I need it to. I need to have access to the customer's default shipping address within the observer, which I load via:

$defaultShipping = $customer->getPrimaryShippingAddress( );

This works fine, except when the user first registers; the $defaultShipping is null (I am assuming at this point is because the address itself hasn't been saved yet). I have tried to overcome this by explicitly saving both the customer as well as all of the customer's addresses if $defaultShipping is null:

if( !$defaultShipping ) {
    $customer->save( );
    foreach( $customer->getAddresses( ) as $address ) {
        // Explicitly save each address (at this point there should only be one,
        // but looping just to be safe).  This should guarantee an ID for it 
        // exists
         $address->save( );
    }

    $defaultShipping = $customer->getPrimaryShippingAddress( );
}

This however is still giving null for $defaultShipping. Am I doing something wrong, or is there another way to load the default shipping address? Does the customer not get a default shipping address if they just registered (should I instead just choose an address from the collection if $defaultShipping is initially null?)

1

Turns out I was a bit off. The default shipping is not actually set for newly created customers; the addresses themselves are already saved, as can be demonstrated here (within the same observer method):

foreach( $customer->getAddresses( ) as $address ) {
     echo $address->getId( );
}
die;

To fix my issue, if the $defaultShipping value was null I simply took the first address out of the customer address collection as my 'default shipping' to use. If anyone has any better solution, feel free to share.

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.