cancel
Showing results for 
Search instead for 
Did you mean: 

How To Overidde customer model ?

How To Overidde customer model ?

Hi All,

 

I am trying to override magento customer model but it is not working. Below are the steps I followed. Can anyone please help ?

In the di.xml added below code

 

<preference for="Magento\Customer\Model\Customer" type="Vendor\Customer\Model\CustomerModel"/>



And in the Vendor/Customer/Model/CustomerModel.php file I put the below content.

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

namespace Vendor\Customer\Model;

use Magento\Customer\Model\Customer;


class CustomerModel extends Customer
{
/**
* 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 _sendEmailTemplate($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@mailinator.com')
->getTransport();

$transport->sendMessage();

return $this;
}
}



But this function is not getting called, My purpose is to add a bcc in the email. Any help is really appreciated.

Thank you

14 REPLIES 14

Re: How To Overidde customer model ?

Hi @sayanth_k 

You code seems correct. 
Just run below command after changes with di.xml:

rm -rf generated/*
sudo chmod -R 777 var/ pub/ generated/
php bin/magento setup:di:compile
php bin/magento cache:flush

It may help you to resolve issue.

If issue resolve, please click on 'Kudos' & Accept as Solution!

Problem solved? Click Accept as Solution!

Re: How To Overidde customer model ?

Hi I added a constructor

 

public function __constrcut()
{
    echo "Here"; exit;
}

This function is getting called, but not the _sendEmailTemplate() function.

Re: How To Overidde customer model ?

Hi @sayanth_k 

As you have shared code with 

protected function _sendEmailTemplate

You are try to override protected function. 
Please try with public function.

Problem solved? Click Accept as Solution!

Re: How To Overidde customer model ?

@sayanth_k 

 

You cannot override protected functions, try to do this with help of around plugin.

 

here you can find how to use around plugin : https://devdocs.magento.com/guides/v2.4/extension-dev-guide/plugins.html#around-methods

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

Re: How To Overidde customer model ?

Hello @sayanth_k 

 

have you added that di.xml into frontend area, then move to global and check it

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer

Re: How To Overidde customer model ?

Could you please tell me how to do this with plugin ? I am new to magento

Re: How To Overidde customer model ?

@sayanth_k 

 

My Bad !
we cannot add plugin for non-public methods
you can either override the function which is calling _sendEmailTemplate or you can simply add one more email in configuration at Send Email Copy To

Can you give more detail for which process you want to add BCC.

 

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

Re: How To Overidde customer model ?

@gaurav_harsh1Actually I want to add the bcc for forgotpassowrd email, My client says that he is not getting the email for forgotpassword. But we are not able to reproduce the issue. So to prove that there is no issue with the email sending. I need to put bcc in that mail ony.

Re: How To Overidde customer model ?

Hello @sayanth_k 

 

you can create patch and apply it 

 

When your work is done, revert back that patch

 

Hope it will help you

 


Problem solved? Click Kudos & Accept as Solution!
Sunil Patel
Magento 2 Certified Professional Developer & Frontend Developer