My di.xml file

<?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\AddressRepository" type="Maha\CreateLastname\Model\ResourceModel\AddressRepository" />

AddressRepository.php file(override model file)

    namespace Maha\CreateLastname\Model\ResourceModel; 
    class AddressRepository extends \Magento\Customer\Model\ResourceModel\AddressRepository
        private function _validate(CustomerAddressModel $customerAddressModel)  {
            echo "Model Rewrite Working";

I want override the private function in this code. What can I do? I want to change anything in my code


Private functions cannot overridden in child class.

If you want to change the logic in _validate function you can use below method.

Override save function, inside that instead of calling $this->_validate($addressModel) function use your own custom function, like below

private function _validateCustom(CustomerAddressModel $customerAddressModel) {
//your custom validation logic

Change the di.xml into

<?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\Api\AddressRepositoryInterface" type="Maha\CreateLastname\Model\ResourceModel\AddressRepository" />

Because in the AccountManagement.php they injected AddressRepositoryInterface in __constructor.

You can achieve this by using plugin as well and that is recommended way.

  • but it's not working.please explain code briefly – Mano M Nov 9 '17 at 7:39
  • Can you update the question with the latest code which you tried – MeenakshiSundaram R Nov 9 '17 at 7:48
  • <?php namespace Maha\CreateLastname\Plugin\Customer\Model\ResourceModel; class AddressRepository { public function save() { echo "save form"; } private function _validate(CustomerAddressModel $customerAddressModel) { echo "hi";exit; } } ?> – Mano M Nov 9 '17 at 8:26
  • is the execution is going into save function?? – MeenakshiSundaram R Nov 9 '17 at 8:43
  • 1
    One more doubt,admin panel->allcustomer->edit->address....throw error last name is required field – Mano M Nov 9 '17 at 13:11

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.