0

In Magento from admin panel we can delete customer and ca do mass delete of customers. Is there any way to run an observer before deleting customer we can get the customer id ?

4

You can use the around plugin here.

app/code/Vendor/Module/etc/di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Customer\Model\ResourceModel\CustomerRepository">
        <plugin name="customer_delete_action" type="Vendor\Module\Plugin\DeleteCustomer" />
    </type>

</config>

app/code/Vendor/Module/Plugin/DeleteCustomer.php

<?php

namespace Vendor\Module\Plugin;

class DeleteCustomer
{
    public function aroundDeleteById(
        \Magento\Customer\Model\ResourceModel\CustomerRepository $subject,
        \Closure $proceed,
        $customerId
    ) {
        // You can add your logic here
        if ($customerId == 10) {
            // Play here
        }

        // It will proceed ahead with the default delete function        
        $result = $proceed($customerId);

        return $result;
    }
}
1

You can try using event customer_delete_before
And in the observer, you can get the object data using

$object = $observer->getData('object');

And you can get the customer id using

$object->getEntityId();

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.