3

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" />
</config> 

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 '16 at 5:39
12

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" />
</config>

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

<?php
namespace Namespace\Modulename\Model\ResourceModel;

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


    protected function _beforeSave(\Magento\Framework\DataObject $customer)
    {
        exit('test');
        //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 '16 at 6:11
  • post your error – Prashant Valanda Mar 10 '16 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 '16 at 6:23
  • add your code and error screen shot to your question – Prashant Valanda Mar 10 '16 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 '17 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.