0

Although I have seen many examples of overriding a function in Magento2, I still cannot manage to make the following work:

Disabling the Welcome-Email for new customers.

The Welcome-Email is sent by the newAccount() function in $MAGENTO_HOME/vendor/magento/module-customer/Model/EmailNotification.php. The goal is to override this function with a empty body such that no Email is sent.

For that purpose, a new module "MyCustomer" was created in $MAGENTO_HOME/app/code/MyModules/.

I also added the following files:

$MAGENTO_HOME/app/code/MyModules/MyCustomer/registration.php:

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'MyModules_MyCustomer',
    __DIR__
);

$MAGENTO_HOME/app/code/MyModules/MyCustomer/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">
    <preference for="Magento\Customer\Model\EmailNotification" type="MyModules\MyCustomer\Model\Rewrite\EmailNotification" />
</config>

$MAGENTO_HOME/app/code/MyModules/MyCustomer/etc/module.xml:

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="MyModules_MyCustomer" setup_version="1.1.0">
    </module>
</config>

The actual file is located at:

$MAGENTO_HOME/app/code/MyModules/MyCustomer/Model/Rewrite/EmailNotification.php:
<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace MyModules\MyCustomer\Model\Rewrite;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Framework\Mail\Template\TransportBuilder;
use Magento\Customer\Helper\View as CustomerViewHelper;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Framework\Reflection\DataObjectProcessor;
use Magento\Framework\Exception\LocalizedException;

/**
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 */
class EmailNotification extends \Magento\Customer\Model\EmailNotification
{
    /**
     * Send email with new account related information
     *
     * @param CustomerInterface $customer
     * @param string $type
     * @param string $backUrl
     * @param string $storeId
     * @param string $sendemailStoreId
     * @return void
     * @throws LocalizedException
     */
    public function newAccount(
        CustomerInterface $customer,
        $type = self::NEW_ACCOUNT_EMAIL_REGISTERED,
        $backUrl = '',
        $storeId = 0,
        $sendemailStoreId = null
    ) {

    }
}

The issue is, that still, the original function is executed and the welcome email is sent. Log files seem to be clean. After code changes were made, the following script was executed:

php bin/magento setup:upgrade
php bin/magento setup:di:compile

php bin/magento indexer:reindex

php bin/magento cache:flush
php bin/magento cache:clean

chown -R wwwrun:www $MAGENTO_HOME

The module seems to be successfully registered as it appears in $MAGENTO_HOME/app/etc/config.php

  • You have to be declare MyModules_MyCustomer instead of My_Customer, please check whether your module is created or not – Sairam Sigirisetty May 30 '18 at 16:24
  • @SairamSigirisetty Thank you, I just checked and that was a typo. In my code is correct with other names. – nck Jun 4 '18 at 7:47
3

Check module naming mistakes in following files

app/code/MyModules/MyCustomer/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'MyModules_MyCustomer',
    __DIR__
);

app/code/MyModules/MyCustomer/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="MyModules_MyCustomer" setup_version="1.1.0">
    </module>
</config>
  • Thank you, I just checked and that was a typo. In my code is correct with other names. – nck Jun 4 '18 at 7:48
  • I mean I made the typo writting the question. I still have the problem. – nck Jun 4 '18 at 8:59

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.