1

When fetching a customer's addresses and deleting them in a foreach loop:

$addresses = $customer->getAddresses();

foreach ($addresses as $address) {
    $address->delete();
}

The addresses appear empty in the customer's address book on the frontend and also in the customer admin section. So the address has been deleted, but the relation to the customer still exists.

When taking a look at Mage_Customer_AddressController::deleteAction nothing more seems to happen than what I am doing, but the addresses don't seem to appear as empty addresses anymore.

What am I overlooking?

3
  • 1
    What kind of relation still exists ?
    – Prateek
    Feb 4, 2016 at 15:01
  • 1
    When I go to Customers > Addresses, I see this: dropbox.com/s/zyva7xx4uaxc2e0/…. Seems that the address is still linked to the customer's address book, but empty. Same goes for address book on the frontend. Feb 4, 2016 at 16:57
  • 1
    $customer->cleanAllAddresses(); $customer->save(); Seems to do the trick. Very undocumented, though, and still does not answer why deleting an address leaves a blank address associated with the customer... Feb 4, 2016 at 17:08

1 Answer 1

0

Just an idea... try

$addresses = $customer->getAddresses();

foreach ($addresses as $address) {
    $address->setCustomerId(null)->save();
    $address->delete();
}
1
  • Gives me SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails [...] Feb 4, 2016 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.