I have to override customer ResourceModel's customer.php file in my custom module.
I have tried but its not working.so what is correct way to override Model? I follow below steps to override Model.

I have my own module called Namespace_Module with a di.xml file,

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Model\ResourceModel\Customer"   type="Namespace\Modulename\Model\ResourceModel\Customer" />

Then I have created php file, Namespace\Modulename\Model\ResourceModel\Customer.php and try to override core file.but its not working for me.

So please anyone help me to solve my problem.

  • You must not do this. What do you want achieved?
    – KAndy
    Mar 10, 2016 at 5:39
  • I see the question was asked long back, but the issues are still relevant. In my case, I want to remove the Required validation of "Email" and there is no way to do that other than overriding the Resource model at vendor/magento/module-customer/Model/ResourceModel/Customer.php. The thing is that in _beforeSave() they explicitly check for whether the customer have Email and fails validation if not. My question is what is the point of 'is_required' property for the email in eav_attribute model? Anyone can create a disposable email nowadays and think about the spam registration you may get!
    – Harish ST
    Aug 17, 2021 at 4:09

1 Answer 1


your file Namespace\Modulename\etc\di.xml code as below

<?xml version="1.0"?>    
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<preference for="Magento\Customer\Model\ResourceModel\Customer" type="Namespace\Modulename\Model\ResourceModel\Customer" />

Your Model code file Namespace\Modulename\Model\ResourceModel\Customer.php

namespace Namespace\Modulename\Model\ResourceModel;

class Customer extends \Magento\Customer\Model\ResourceModel\Customer

    protected function _beforeSave(\Magento\Framework\DataObject $customer)
        //your code here

hope it will helpful to you

  • Thanks for your quick response. I have tried this code and this code is working,it will call custom module's _beforeSave action but I want to comment $this->_validate($customer); line for my custom module.but it will not working.It give error
    – Pooja
    Mar 10, 2016 at 6:11
  • post your error Mar 10, 2016 at 6:17
  • "First Name" is a required value. "First Name" length must be equal or greater than 1 characters. "Last Name" is a required value. "Last Name" length must be equal or greater than 1 characters. "My Attribute" is a required value. #0 C:\xampp\htdocs\magento2\vendor\magento\module-customer\Model\ResourceModel\Customer.php(143): Magento\Customer\Model\ResourceModel\Customer->_validate(Object(Magento\Customer\Model\Customer))C:\xampp\htdocs\magento2\app\code\Explorer\Logindialog\Model\ResourceModel\Customer.php:Magento\Customer\Model\ResourceModel\Customer I got this error.
    – Pooja
    Mar 10, 2016 at 6:23
  • add your code and error screen shot to your question Mar 10, 2016 at 8:41
  • I am doing same things to override shipping Model file but it's not working and only display Loader.
    – Magecode
    Nov 6, 2017 at 9:44

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.