0

enter image description here

In the above attach file I want to change

Magento\SalesRule\Model\Rule\Condition\Address

to

Company\SalesRule\Model\Rule\Condition\Address

0

Create di.xml file in Company\SalesRule\etc folder

 <?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\SalesRule\Model\Rule\Condition\Address" type="Company\SalesRule\Model\Rule\Condition\Address" />
</config>

Create Address.php file in Company\SalesRule\Model\Rule\Condition folder

<?php

 namespace Company\SalesRule\Model\Rule\Condition;

class Address extends \Magento\SalesRule\Model\Rule\Condition\Address
{
    // Do your stuff here
}
0

You can follow below step to override Magento\SalesRule\Model\Rule\Condition\Address

File path: magento/app/code/Company/SalesRule/etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Company_SalesRule" setup_version="1.0.0">
    </module>
</config>

File path: magento/app/code/Company/SalesRule/registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Company_SalesRule',
__DIR__
);

File path: magento/app/code/Company/SalesRule/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\SalesRule\Model\Rule\Condition\Address" type="Company\SalesRule\Model\Rule\Condition\Address" />
</config>

File path: magento/Company/SalesRule/Model/Rule/Condition/Address.php

<?php

namespace Company\SalesRule\Model\Rule\Condition;

class Address extends \Magento\Rule\Model\Condition\AbstractCondition
{
    /* You can write code hear as you want to need changes */

}
  • I have already tried this, but not working as the function validate() in which want to make change is added in Magento\SalesRule\Model\Rule\Condition\Address file & it doesn't get called on checkout page – Arjun Sep 5 at 6:48
  • Can you try to put complete Address.php file and just modify function validate() and check. – Kirti Nariya Sep 5 at 7:00
  • I tried that too but not working. – Arjun Sep 5 at 8:22
0

Create a basic module as Company/SalesRule

Step 1 : Add di.xml file inside Company/SalesRule/etc folder with below content -

Step 2 : Create Address.php with below content inside Company/SalesRule/Model/Rule/Condition folder

class Address extends \Magento\Rule\Model\Condition\AbstractCondition { ** Write your methods here }

0

You can setup plugin instead of using preferences because validate() is public function and you can write plugin on it.

When setting up a new plugin for a class object, it will be defined in the di.xml file at app/code/{namespace}/{module}/etc/di.xml.Put the following code in di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\SalesRule\Model\Rule\Condition\Address">
       <plugin name="Company_Salesrule_Address_Plugin" type="Company\SalesRule\Plugin\Rule\Condition\Address" sortOrder="10" disabled="false"  />
    </type>
</config>

You need to create file Address.php at Company\SalesRule\Plugin\Rule\Condition\ and add the following contents:

<?php

 namespace Company\SalesRule\Plugin\Rule\Condition\;

 class Address extends \Magento\Framework\App\Action\Action
 {
    //After methods take a responsibility of editing the results of an observed method
    public function afterValidate(Magento\SalesRule\Model\Rule\Condition\Address $subject, $result)
    {
          //Do your stuff here if you want to modify the result of validate()
    }

    //To apply the before methods for modifying the arguments of an observed method, you can return a modified argument.
    public function beforeValidate(Magento\SalesRule\Model\Rule\Condition\Address $subject, $customParameter)
    {
          //Do your stuff here if you want to modify the arguments of validate() before execution of it.
    }

   //Around methods allows the code to run before and after the observed method, so you can override a method.
    public function aroundValidate(Magento\SalesRule\Model\Rule\Condition\Address $subject, callable $proceed)
    {
          //Do your stuff here if you want to modify the validate() method.
    }

 }

You can use any one the before, after, around method to achieve your requirements.

Refer this for more information for plugin https://www.mageplaza.com/magento-2-module-development/magento-2-plugin-interceptor.html

I hope this solution work for you.

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.