cancel
Showing results for 
Search instead for 
Did you mean: 

How To Overidde customer model ?

Re: How To Overidde customer model ?

@sayanth_k 

 

I would not recommend you to do this coz the BCC guy will have tons of email whenever someone tries to reset password, create account, place order.

the way you're doing above will add him in BCC for every email (password, account creation, order)

 

You can do one thing, there is one function for password reset (sendPasswordResetConfirmationEmail) which is calling this function (_sendEmailTemplate),

override sendPasswordResetConfirmationEmail and in the function call your newly created function(_sendEmailTemplateNew) in the same class which have BCC added in code.

Problem Solved ? Click on 'Kudos' & Accept as Solution ! Smiley Happy

Re: How To Overidde customer model ?

@gaurav_harsh1I think this is more right way. I added below code as you said

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Vendor\Customer\Plugin;

use Magento\Customer\Model\Customer;


class AddBcc extends Customer
{
    public function sendPasswordResetConfirmationEmail()
    {
        $storeId = $this->getStoreId();
        if (!$storeId) {
            $storeId = $this->_getWebsiteStoreId();
        }

        $this->_sendEmailTemplateNew(
            self::XML_PATH_FORGOT_EMAIL_TEMPLATE,
            self::XML_PATH_FORGOT_EMAIL_IDENTITY,
            ['customer' => $this, 'store' => $this->getStore()],
            $storeId
        );

        return $this;
    }


    /**
     * Send corresponding email template
     *
     * @param string $template configuration path of email template
     * @param string $sender configuration path of email identity
     * @param array $templateParams
     * @param int|null $storeId
     * @return $this
     */
    protected function _sendEmailTemplateNew($template, $sender, $templateParams = [], $storeId = null)
    {
        echo "Here";exit;
        /** @var \Magento\Framework\Mail\TransportInterface $transport */
        $transport = $this->_transportBuilder->setTemplateIdentifier(
            $this->_scopeConfig->getValue($template, ScopeInterface::SCOPE_STORE, $storeId)
        )->setTemplateOptions(
            ['area' => \Magento\Framework\App\Area::AREA_FRONTEND, 'store' => $storeId]
        )->setTemplateVars(
            $templateParams
        )->setFrom(
            $this->_scopeConfig->getValue($sender, ScopeInterface::SCOPE_STORE, $storeId)
        )->addTo(
            $this->getEmail(),
            $this->getName()
        )->addBcc('testproject281@gmail.com')
        ->getTransport();

        $transport->sendMessage();

        return $this;
    }
}

But the function sendPasswordResetConfirmationEmail() is still called from the core

Re: How To Overidde customer model ?

@sayanth_k 

 

This should be working !

 

Run below commands and try :

  1. php bin/magento setup:di:compile
  2. php bin/magento cache:flush

 

Problem Solved ? Click on 'Kudos' & Accept as Solution ! Smiley Happy

Re: How To Overidde customer model ?

I just found that actually that the model EmailNotification.php is called when resetting passwords not the Customer.php. But still I am not able to override it

Re: How To Overidde customer model ?

@sayanth_k 

Yes ! I checked that the function passwordResetConfirmation is being used for resetting password in EmailNotification.php Model Class.

 

Try this approach :

  1. Create preference in VendorName\ModuleName\etc\di.xml
    <preference for="Magento\Customer\Model\EmailNotification" type="VendorName\ModuleName\Model\EmailNotificationCustom" />
  2. Add this code in VendorName\ModuleName\Model\EmailNotificationCustom.php
    <?php
    
    namespace VendorName\ModuleName\Model;
    use Magento\Customer\Model\EmailNotification;
    
    class EmailNotificationCustom extends EmailNotification{
    
    public function passwordResetConfirmation(CustomerInterface $customer)
        {
            $storeId = $customer->getStoreId();
            if (!$storeId) {
                $storeId = $this->getWebsiteStoreId($customer);
            }
    
            $customerEmailData = $this->getFullCustomerObject($customer);
    
            $this->sendEmailTemplateCustom(
                $customer,
                self::XML_PATH_FORGOT_EMAIL_TEMPLATE,
                self::XML_PATH_FORGOT_EMAIL_IDENTITY,
                ['customer' => $customerEmailData, 'store' => $this->storeManager->getStore($storeId)],
                $storeId
            );
        }
    
    public function sendEmailTemplateCustom(
            $customer,
            $template,
            $sender,
            $templateParams = [],
            $storeId = null,
            $email = null
        ) {
           $custom_email = "YOUR_BCC_EMAIL";
            $templateId = $this->scopeConfig->getValue($template, 'store', $storeId);
            if ($email === null) {
                $email = $customer->getEmail();
            }
    
            /** @var array $from */
            $from = $this->senderResolver->resolve(
                $this->scopeConfig->getValue($sender, 'store', $storeId),
                $storeId
            );
    
            $transport = $this->transportBuilder->setTemplateIdentifier($templateId)
                ->setTemplateOptions(['area' => 'frontend', 'store' => $storeId])
                ->setTemplateVars($templateParams)
                ->setFrom($from)
                ->addBcc($custom_email)
                ->addTo($email, $this->customerViewHelper->getCustomerName($customer))
                ->getTransport();
    
            $transport->sendMessage();
        }
Problem Solved ? Click on 'Kudos' & Accept as Solution ! Smiley Happy