0

Hi I referred this solution for disabling welcome email and it works perfectly.however i want the around plugin to work only if its enabled from admin backend that too in store basis.

I added etc/adminhtml/system.xml

<?xml version="1.0" ?>


<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>     
        <section id="customer" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <group id="create_account" translate="label" type="text" sortOrder="20" showInDefault="1" showInWebsite="1" showInStore="1">
                <field id="welcome_email_disable" translate="label" type="select" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Disable Welcome Email</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

etc/config.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <customer>
            <create_account>
                <welcome_email_disable>1</welcome_email_disable>
            </create_account>
        </customer>
    </default>
</config>

etc/di.xml

<?xml version="1.0" encoding="utf-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="\Magento\Customer\Model\EmailNotification">
        <plugin name="disable-email-notification" type="Vendor\moduleName\Plugin\EmailNotification" sortOrder="1"/>
    </type>

</config>

Helper/Data.php

<?php


declare(strict_types=1);

namespace Vendor\moduleName\Helper;

use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Store\Model\ScopeInterface;
use Magento\Framework\App\Helper\AbstractHelper;

class Data extends AbstractHelper
{
   
    protected $scopeConfig;

    /**
     * Extension enabled config path
     */
    protected const XML_PATH_NO_WELCOME_EMAIL = 'customer/create_account/welcome_email_disable';

    /**
     * Constructor
     *
     * @param ScopeConfigInterface $scopeConfig
     */
    public function __construct(
        ScopeConfigInterface $scopeConfig
    ) {
        $this->scopeConfig = $scopeConfig;
    }
    
    /**
     * Retrieve true if extension is enabled.
     *
     * @return bool
     */
    public function moduleEnabled()
    {
       
        return (bool) $this->scopeConfig->getValue(
            self::XML_PATH_NO_WELCOME_EMAIL,
            \Magento\Store\Model\ScopeInterface::SCOPE_STORE
        );
    }
}

Plugin/EmailNotification.php

<?php

namespace Vendor\moduleName\Plugin;
use Vendor\moduleName\Helper\Data;
class EmailNotification
{
    public function __construct(
        Data $moduleEnable
    ) {
        $this->moduleEnable = $moduleEnable;
    }

    
        public function aroundNewAccount(\Magento\Customer\Model\EmailNotification $subject, \Closure $proceed)
        {
             if ($this->moduleEnable->moduleEnabled() ===  1) {
                return $subject;
           }
            
        }
   
}

even though i am getting the value from moduleEnabled() in EmailNotification.php, this is not working properly and new customers created through admin are not visible when trying this code out.Could anyone help me with this ? I needed to enable/disable this feature from backend admin configuration in store basis.

1 Answer 1

0

This is I think due to a php error

=== operator makes a strict comparison type.

so what is happening is that you make

true === 1 // false
true == 1 // true

So either you compare with the value true, either you remove the bool cast from your method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.