0

I have created a custom module to disable free shipping based on some conditions. I have to used

Custom/ShippingRule/Plugin/Magento/Shipping/Model/Shipping.php to do the functionality.

It is working fine now. I want to enable/disable this from admin.

3 Answers 3

0

I am not sure about the 100% solution. But might below one can be a solution.

Suppose your di.xml which is exist at app/code/{vendor}/{moduleFolder}/etc folder.

<?xml version="1.0"?>
<config>
    <type name="{ObservedType}">
      <plugin name="my_plugin_name" type="{PluginClassName}" sortOrder="1" disabled="false" />
    </type>
</config>

To disable this plugin at adminhtml area, create di.xml at app/code/{vendor}/{moduleFolder}/etc/adminhtml and disabled that plugin by plugin name my_plugin_name like below:\

<?xml version="1.0"?>
<config>
    <type name="{ObservedType}">
      <plugin name="my_plugin_name"  disabled="true" />
    </type>
</config>
1
  • I want to disable from admin not programatically.
    – Ramesh KR
    Commented Sep 30, 2020 at 8:14
0

For that you have to create system.xml file in your module. Path for this file is app/code/{vendor}/{modulename}/etc/adminhtml/system.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
    <system>
        <tab id="tab_id" translate="label" sortOrder="10">
            <label>{vendor}</label>
        </tab>
        <section id="sectionid" translate="label" sortOrder="130" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <label>XYZ</label>
            <tab>tab_id</tab>
            <resource>Vendor_Modulename::sectionid_config</resource>
            <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>General Configuration</label>
                <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Module Enable</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
            </group>
        </section>
    </system>
</config>

Now you have to set default value for that create config.xml file at app/code/{vendor}/{modulename}/etc/config.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <sectionid>
            <general>
                <enable>1</enable>
                <display_text>XYZ</display_text>
            </general>
        </sectionid>
    </default>
</config>

This will display configuration in admin. After that you have to fetch value of enable/disable in controller using helper.

0

You can apply your code under this below condition.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$state = $objectManager->get('Magento\Framework\App\State');
if($state->getAreaCode() != 'backend') 
{
  /* apply your code in here */
}

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.