4

We want to create one custom extension which has functionality in admin side for Enable/Disable. Any one help me how to add this functionality in my custom extension.

2
  • How you want to use this setting is the main thing? Custom Plugin, Events, Preferences still will be executed.
    – MagePsycho
    Commented Jun 13, 2019 at 13:39
  • We use this custom plugin only for overwrite phtml files of product.
    – Anil
    Commented Jun 13, 2019 at 14:07

3 Answers 3

1

For this, you need to Create a System.xml in the folder like 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="ModuleName" translate="label" sortOrder="10">
           <label>ModuleName</label>
       </tab>

      <section id="customproductdata" translate="label" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
          <class>separator-top</class>
            <label>Custom Product Data</label>
                <tab>ModuleName</tab>
    <resource>ModuleName_Customproductdata::customproductdata_configuration</resource>
            <group id="general" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
            <label>General Configuration</label>
           <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
          <label>Enable</label>
         <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
     </field>
   </group>
 </section>
</system>

 
Than Create Config.xml file as the Same location and Put Below code into that.

<?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>
     <customproductdata>
       <general>
           <enable>1</enable>
              <title>ModuleName Customproductdata</title>
         </general>
     </customproductdata>
 </default>
</config>

Now you will See this custom Yes no option in Magento 2 admin section.

2
  • Thanks. It's work perfectly.
    – Anil
    Commented Jun 14, 2019 at 7:34
  • If answer was correct or satisfy with answer please approved answer. Commented Jun 14, 2019 at 9:12
1

Create system.xml in your module.

etc/adminhtml/system.xml

Add the following content to have enable disable module configuration.

<?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>
    <section id="yourmodulename" translate="label" sortOrder="150" showInDefault="1" showInWebsite="1" showInStore="1">
        <resource>Vendor_YourModuleName::configuration</resource>
        <group id="general" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
            <label>Configuration</label>
            <field id="enable" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Enable Module</label>
                <source_model>Magento\Config\Model\Config\Source\Enabledisable</source_model>
            </field>
        </group>
    </section>
</system>
</config>
1
  • In admin site where this setting show ? Enable or Disable Commented Jun 13, 2019 at 14:04
1

Add below code in etc/adminhtml/system.xml

<group id="general" showInDefault="1" showInWebsite="1" showInStore="1" sortOrder="20">
<field id="enabled" translate="label" type="select" sortOrder="10" showInDefault="1">
    <label>Enable</label>
    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>    
</field>
</group>

Using below code in helper file.

public function isEnabled()
{
    return (boolean) $this->getConfig('namespace_modulename/general/enable');
}

And call this function whenever you want. That's it, i hope it helps!

3
  • If any possible way to create extension with enable/disable option ?
    – Anil
    Commented Jun 13, 2019 at 14:44
  • I have a post answer for that. but I think you are confused. can you explain more what do you want exactly? Commented Jun 14, 2019 at 4:38
  • we want to create one custom extension. Which in manage from admin side. It means any person enable/disable it from admin side. Main purpose for create this extension is overwrite phtml files for product view page.
    – Anil
    Commented Jun 14, 2019 at 5:53

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.