Netorica
Netorica

Reputation: 19327

Delete a Customer Progmatically in Magento

How can I delete a customer in magento by refering or using its object from Mage::getModel("customer/customer") ?

I already researched in google and with the bad luck I found nothing.

Upvotes: 1

Views: 666

Answers (3)

Netorica
Netorica

Reputation: 19327

I find it out. customer is an EAV like products that can be deleted using delete() method. Just flag the object to be deleteable using $customer->setIsDeleteable(true) because the customer model class which is Mage_Customer_Model_Customer implements Mage_Core_Model_Abstract which contains the delete() method

I used the following codes below

$customer->loadByEmail('[email protected]');
$customer->setIsDeleteable(true);
$customer->delete();

if you want the deletion to workout in the frontend(in my case its backend) you need to initiate the Mage::register('isSecureArea', true)

Upvotes: 2

Ravi Patel
Ravi Patel

Reputation: 5211

<?php

        //current user delete using email
        $customer = Mage::getModel("customer/customer");
        Mage::register('isSecureArea', true);     
        $email = Mage::getSingleton('customer/session')->getCustomer()->getEmail();
        $customer->loadByEmail($email);
        $customer->setIsDeleteable(true);
        $customer->delete();

    ?>

Upvotes: 0

raheem.unr
raheem.unr

Reputation: 877

Please find the below solution

$sessCustomer = Mage::getSingleton('customer/session')->getCustomer();
$customer = Mage::getModel('customer/customer');        
$customer->setWebsiteId(Mage::app()->getStore()->getWebsiteId());
// load by customer id
$customer->load($sessCustomer->getId()); 
try {       
        Mage::register('isSecureArea', true);       
        $customer->delete();                            
    }catch (Exception $e){
        $e->getMessage();
}

Upvotes: 1

Related Questions