cancel
Showing results for 
Search instead for 
Did you mean: 

Send mail to all customer when customer csv imported

Send mail to all customer when customer csv imported

Hello All,

Please follow the below code for sending mail to all customer when customer csv imported, for that override the core functionality

  1. First override the core functionality in your custom module using di.xml file. Create a di.xml file in app/etc folder
  2. <?xml version="1.0"?>
    
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
       <preference for="Magento\CustomerImportExport\Model\Import\Customer" type="CompanyName\ModuleName\Model\Import\Customer" />
    </config>
  3. Go to CompanyName/ModuleName/Model/Import/Customer.php and search _importData() function and write code for sending email to all customer.
     protected function _importData()
        {
            while ($bunch = $this->_dataSourceModel->getNextBunch()) {
                $entitiesToCreate = [];
                $entitiesToUpdate = [];
                $entitiesToDelete = [];
                $attributesToSave = [];
    
                foreach ($bunch as $rowNumber => $rowData) {
                    $email = $rowData['email'];
                    //Here write custom code for sending email to all customer
                    //or you can call another helper file or controller file for sending email
                    if (!$this->validateRow($rowData, $rowNumber)) {
                        continue;
                    }
                    if ($this->getErrorAggregator()->hasToBeTerminated()) {
                        $this->getErrorAggregator()->addRowToSkip($rowNumber);
                        continue;
                    }
    
                    if ($this->getBehavior($rowData) == \Magento\ImportExport\Model\Import::BEHAVIOR_DELETE) {
                        $entitiesToDelete[] = $this->_getCustomerId(
                            $rowData[self::COLUMN_EMAIL], $rowData[self::COLUMN_WEBSITE]
                        );
                    } elseif ($this->getBehavior($rowData) == \Magento\ImportExport\Model\Import::BEHAVIOR_ADD_UPDATE) {
                        $processedData = $this->_prepareDataForUpdate($rowData);
                        $entitiesToCreate = array_merge($entitiesToCreate, $processedData[self::ENTITIES_TO_CREATE_KEY]);
                        $entitiesToUpdate = array_merge($entitiesToUpdate, $processedData[self::ENTITIES_TO_UPDATE_KEY]);
                        foreach ($processedData[self::ATTRIBUTES_TO_SAVE_KEY] as $tableName => $customerAttributes) {
                            if (!isset($attributesToSave[$tableName])) {
                                $attributesToSave[$tableName] = [];
                            }
                            $attributesToSave[$tableName] = array_diff_key(
                                    $attributesToSave[$tableName], $customerAttributes
                                ) + $customerAttributes;
                        }
                    }
                }
                $this->updateItemsCounterStats($entitiesToCreate, $entitiesToUpdate, $entitiesToDelete);
                /**
                 * Save prepared data
                 */
                if ($entitiesToCreate || $entitiesToUpdate) {
                    $this->_saveCustomerEntities($entitiesToCreate, $entitiesToUpdate);
                }
                if ($attributesToSave) {
                    $this->_saveCustomerAttributes($attributesToSave);
                }
                if ($entitiesToDelete) {
                    $this->_deleteCustomerEntities($entitiesToDelete);
                }
            }
    
            return true;
        }
Certified Magento 2 Developer
Vivek Singh