Customer model file override issue in Magento2

by jafar pinjar   Last Updated October 09, 2018 06:09 AM

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, Please anyone suggest on this. Thanks in Advance.



Answers 1


Try changing di.xml as :

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

Also perform setup:upgrade.

Are you getting any error ?

Narendra
Narendra
October 09, 2018 06:05 AM

Related Questions


How to override customer resource module customer.php

Updated November 22, 2017 07:09 AM

Overriding private method of Model file in Magento 2

Updated October 09, 2018 11:09 AM

How to send email through programetically in magento 2.0?

Updated September 16, 2016 09:03 AM