How to Disable Customer Welcome Email Only Using preference not with plugin
1 Answer
You can create your preference like so:
<?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="Vendor\Module\Model\NoEmailNotification" />
</config>
and then override the newAccount
method as follows:
<?php
namespace Vendor\Module\Model\Onepage;
class NoEmailNotification extends \Magento\Customer\Model\EmailNotification
{
/**
* Don't send email with new account related information
*
* @param CustomerInterface $customer
* @param string $type
* @param string $backUrl
* @param int|null $storeId
* @param string $sendemailStoreId
* @return void
* @throws LocalizedException
*/
public function newAccount(
CustomerInterface $customer,
$type = self::NEW_ACCOUNT_EMAIL_REGISTERED,
$backUrl = '',
$storeId = null,
$sendemailStoreId = null
): void {
return;
}
}
Which essentially inverts the original functionality of the method linked here (https://github.com/magento/magento2/blob/cd826aa2215e19d543a3a606aebde18fa3e40d4e/app/code/Magento/Customer/Model/EmailNotification.php#L384)
So the customer no longer receives a welcome email
Magento\Customer\Model\EmailNotificationInterface
. But I suggest to use plugin for the interface and usearound
with correctsortOrder
for prevent to replace your logic by 3rd party extension like DotMailer, etc.