2

I am trying to override customer model file below is the code of my di.xml

 <preference for = "Magento\Customer\Model\AccountManagement" type = "Vendor\Module\Model\Rewrite\Customer\AccountManagement" />

Below is code for my model file

 <?php
 namespace Vendor\Module\Model\Rewrite\Customer;

 use Magento\Customer\Api\Data\CustomerInterface;

class AccountManagement extends \Magento\Customer\Model\AccountManagement
{


public function createAccountWithPasswordHash(CustomerInterface $customer, $hash, $redirectUrl = '')
{
    // This logic allows an existing customer to be added to a different store.  No new account is created.
    // The plan is to move this logic into a new method called something like 'registerAccountWithStore'
    if ($customer->getId()) {
        $customer = $this->customerRepository->get($customer->getEmail());
        $websiteId = $customer->getWebsiteId();

        if ($this->isCustomerInStore($websiteId, $customer->getStoreId())) {
            throw new InputException(__('This customer already exists in this store.'));
        }
        // Existing password hash will be used from secured customer data registry when saving customer
    }

    // Make sure we have a storeId to associate this customer with.
    if (!$customer->getStoreId()) {
        if ($customer->getWebsiteId()) {
            $storeId = $this->storeManager->getWebsite($customer->getWebsiteId())->getDefaultStore()->getId();
        } else {
            $storeId = $this->storeManager->getStore()->getId();
        }
        $customer->setStoreId($storeId);
    }

    // Associate website_id with customer
    if (!$customer->getWebsiteId()) {
        $websiteId = $this->storeManager->getStore($customer->getStoreId())->getWebsiteId();
        $customer->setWebsiteId($websiteId);
    }

    // Update 'created_in' value with actual store name
    if ($customer->getId() === null) {
        $storeName = $this->storeManager->getStore($customer->getStoreId())->getName();
        $customer->setCreatedIn($storeName);
    }

    $customerAddresses = $customer->getAddresses() ?: [];
    $customer->setAddresses(null);
    try {
        // If customer exists existing hash will be used by Repository
        $customer = $this->customerRepository->save($customer, $hash);
    } catch (AlreadyExistsException $e) {
        throw new InputMismatchException(
            __('CHANGE THIS TEXT FROM DEFAULT TO CUSTOM ONE.')
        );
    } catch (LocalizedException $e) {
        throw $e;
    }
    try {
        foreach ($customerAddresses as $address) {
             if ($address->getId()) {
                $newAddress = clone $address;
                $newAddress->setId(null);
                $newAddress->setCustomerId($customer->getId());
                $this->addressRepository->save($newAddress);
             } else {
                $address->setCustomerId($customer->getId());
                $this->addressRepository->save($address);
            }
        }
    } catch (InputException $e) {
        $this->customerRepository->delete($customer);
        throw $e;
    }
    $customer = $this->customerRepository->getById($customer->getId());
    $newLinkToken = $this->mathRandom->getUniqueHash();
    $this->changeResetPasswordLinkToken($customer, $newLinkToken);
    //$this->sendEmailConfirmation($customer, $redirectUrl);

    return $customer;
    }  
 }

Above code is not working for me, thrwoing below error.

Notice: Undefined property: Vendor\Module\Model\Rewrite\Customer\AccountManagement\Interceptor::$storeManager in app/code/Vendor/Module/Model/Rewrite/Customer/AccountManagement.php on line 42

All i need to do is i should stop sending emails for new customer registrations. so i have commented out this line

  $this->sendEmailConfirmation($customer, $redirectUrl);

Please anyone suggest on this. Thanks in Advance.

3 Answers 3

1

If you want to stop sending emails only, then you can simply override the sendEmailConfirmation function and return false.

use Magento\Customer\Api\Data\CustomerInterface;

...
...

protected function sendEmailConfirmation(CustomerInterface $customer, $redirectUrl)
{
    return false;
}
6
  • What is the file path of this function? Commented Oct 9, 2018 at 6:57
  • Same file and same path which you are using currently. Commented Oct 9, 2018 at 7:02
  • Thanks it worked, but newsletter subscription and unsubscription both emails are generating Commented Oct 9, 2018 at 8:18
  • Those emails are not dependent on this function. You have to customize their respective functions. Commented Oct 9, 2018 at 9:11
  • Yes, but why both the emails are coming, i selected checkbox Commented Oct 9, 2018 at 9:18
1

Please run below command and check:

rm -rf generated/*

php bin/magento c:c

php bin/magento c:f

This path Vendor\Module\Model\Rewrite\Customer\AccountManagement also fine no need to change

7
  • Checked but i am getting below error once used above code, Notice: Undefined property: Vendor\Module\Model\Rewrite\Customer\AccountManagement\Interceptor::$storeManager in Commented Oct 9, 2018 at 6:18
  • Please paste full error in question @jafarpinjar Commented Oct 9, 2018 at 6:21
  • Yes i am updated my question, @Rutvee Commented Oct 9, 2018 at 6:24
  • @jafarpinjar please check core file where some variable are define protected and some are private.. so don't need to inject protected variable but you need to inject private variable Commented Oct 9, 2018 at 6:33
  • yes i noticed, but its not throwing error for other variables like, mathRandom, addressRepository, these also private Commented Oct 9, 2018 at 6:37
0

I think you haven't injected the proper dependency in your preference:

   <?php

   use Magento\Store\Model\StoreManagerInterface;

   class AccountManagement extends \Magento\Customer\Model\AccountManagement
   {
    /**
     * @var \Magento\Store\Model\StoreManagerInterface
    */
       private $storeManager;

     public function __construct(
        StoreManagerInterface $storeManager,
     ){
        $this->storeManager = $storeManager;
     }

Inject rest dependencys also like this

clear var/*, generated

Also perform setup:upgrade.

Are you getting any error ?

10
  • let me check and confirm Commented Oct 9, 2018 at 6:09
  • Used your code, its not firing, I have commented this line but still emails are triggering for new customers Commented Oct 9, 2018 at 6:17
  • have a look here : magento.stackexchange.com/questions/139456/… Commented Oct 9, 2018 at 6:19
  • That answer is not accepted, have you tried it , is it working? Commented Oct 9, 2018 at 6:20
  • no i havent, if you are getting Undefined property: Vendor\Module\Model\Rewrite\Customer\AccountManagement\Interceptor::$storeManager this error....... this is because you have not injected the dependency in your preference file... just go into the core file and see how $storeManager is injected in constructor... if you will do in same way then it will work Commented Oct 9, 2018 at 6:22

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.