1
votes

I want to delete all the address associated with the customer in the Magento(billing and shipping address). How can i do this programatically? Can somebody help me out?

I am using this code:

    $customer = Mage::getModel('customer/customer');
    $customer->setWebsiteId(Mage::app()->getWebsite()->getId());

    $customer->loadByEmail((string) $_REQUEST['email']);

    $address = Mage::getModel('customer/address');

    $address->delete();
2

2 Answers

3
votes

Assumed that you have loaded the customers from the customer collection. Find the code below to load the customer addresses using the customer ID and delete it one by one.

    if($customer){
         /*Load the customer addresses by Customer Id*/
        $customerAddressCollection = Mage::getResourceModel('customer/address_collection')->addAttributeToFilter('parent_id',$customer->getId())->getItems();
        foreach($customerAddressCollection as $customerAddress){
            $customer_address_id = $customerAddress->getData('entity_id');
            if($customer_address_id!=""){   
      /*Load the Customer Address by ID and delete it*/    
               Mage::getModel('customer/address')->load($customer_address_id)->delete();
            }
        }
    }
0
votes

Do you want to delete the relation or delete all addresses?

Relation

$col = Mage::getModel('customer/customer')->getCollection()
foreach($col as $customer) {
    $address = $customer->getDefaultBillingAddress();
    // set attributes
    $address->save();

    $address = $customer->getDefaultShippingAddress();
    // set attributes
    $address->save();

}

Delete all addresses

This is a bad idea, because there are a lot foreign keys on the addresses. Don't do that.