Magento – Customer delete before observer

customerevent-observermagento2

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 ?

Best Answer

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;
    }
}
Related Topic