Magento – Magento 2: How to override the model

magento2.2modeloverridesphp-7

How to override the model:

vendor/magento/module-Customer/Model/Address/AbstractAddress.php

For removing the validation of the last name. What can I do?

I have already overriden this file using the around plugin method, but it is still throwing an error.

Best Answer

Create di.xml File

Path : app/code/{vendorName}/{ModuleName}/etc/di.xml

<?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\Address\AbstractAddres" type="{vendorName}\{ModuleName}\Model\Address\AbstractAddres" />
</config> 

Create Model file

Path : app/code/{vendorName}/{ModuleName}/Model/Address/AbstractAddres.php

namespace {vendorName}\{ModuleName}\Model\Address;

    class AbstractAddres extends \Magento\Customer\Model\Address\AbstractAddres
    {
        public function __construct()
        {
            echo "Model Rewrite Working"; die();

        }

    }
Related Topic