Showing results for 
Search instead for 
Did you mean: 

Override Model Class

Override Model Class

Hi There,


Can anyone please assist me on how I can override core Mangento Model class specific function please. Class in question here is "\Magento\Customer\Model\AccountManagement" and the function I am trying to override is createAccountWithPasswordHash();

My existing code below which doesn't seem to have any effect on the core Magento instructions.


<?xml version = "1.0"?>
<config xmlns:xsi = "" xsi:noNamespaceSchemaLocation = "urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for = "Magento\Customer\Model\AccountManagement" type = "Abc\CustomerOverride\Model\Rewrite\Customer\AccountManagement" />

namespace Abc\CustomerOverride\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();

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

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

        $customerAddresses = $customer->getAddresses() ?: [];
        try {
            // If customer exists existing hash will be used by Repository
            $customer = $this->customerRepository->save($customer, $hash);
        } catch (AlreadyExistsException $e) {
            throw new InputMismatchException(
        } catch (LocalizedException $e) {
            throw $e;
        try {
            foreach ($customerAddresses as $address) {
                 if ($address->getId()) {
                    $newAddress = clone $address;
                 } else {
        } catch (InputException $e) {
            throw $e;
        $customer = $this->customerRepository->getById($customer->getId());
        $newLinkToken = $this->mathRandom->getUniqueHash();
        $this->changeResetPasswordLinkToken($customer, $newLinkToken);
        //$this->sendEmailConfirmation($customer, $redirectUrl);

        return $customer;