-2

Need to add default text field value in system configuration. Any idea?

Thanks

  • you need to add and save text field into magento 2 admin configuration section using your custom module? am i right? – Bhavin Pethani Jan 9 at 7:19
  • I need to show the default text field value in system configuration field – Shomita Jan 9 at 7:26
  • Check my answer and let me known if it is working or not. – Bhavin Pethani Jan 9 at 10:00
2

Please follow below step to store default text field value.

Change file as follow

app\code\Vendor\Abandon\etc\adminhtml\system.xml

<?xml version="1.0" encoding="UTF-8"?>
 <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Config:etc/system_file.xsd">
<system>
    <tab id="Vendor" translate="label" sortOrder="550">
        <label>Vendor</label>
    </tab>
    <section id="vendor_abandon" showInDefault="1" showInWebsite="0" showInStore="0" type="text" translate="label" sortOrder="100">
        <label>Abandoned Cart Settings</label>
        <tab>vendor</tab>
        <resource>Vendor_Abandon::abandon_settings</resource>

        <group id="abandon_cart" showInDefault="1" showInStore="0" showInWebsite="0" sortOrder="20">
            <label>General Configuration</label>
            <field id="abandon_cart_media_path" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="0" showInStore="0">
                <label>Catalog media files path</label>
            </field>
        </group>
    </section>
</system>

app\code\Vendor\Abandon\etc\config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <vendor_abandon>
            <abandon_cart>
                <abandon_cart_media_path>Test</abandon_cart_media_path>
            </abandon_cart>
        </vendor_abandon>
    </default>
</config>

You can set your default value in <abandon_cart_media_path>Test</abandon_cart_media_path> as per your requirement. i just added as test default value.

  • Ok, But can you tell me why should i delete? – Bhavin Pethani Jan 9 at 10:09
  • I am updated code. Please check. – Bhavin Pethani Jan 9 at 10:14
  • below your question you will get delete option – Biren Patel Jan 9 at 10:21
  • ok , so first you need to tell the the guys to delete their answer from your question – Biren Patel Jan 9 at 10:26
2

Please change file as follow

app\code\Abc\Abandon\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> 

     <section id="abc_abandon" translate="label" type="text" sortOrder="23" showInDefault="1" showInWebsite="1" showInStore="1">
        <group id="abandon_cart" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="1">
          <field id="abandon_cart_media_path" translate="label" type="text" sortOrder="1" showInDefault="1" showInWebsite="0" showInStore="0">
                    <label>Catalog media files path</label>

            </field>
         </group>
    </section> </system> </config>

app\code\Abc\Abandon\etc\config.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <abc_abandon><!-- tag of the system.xml section -->
            <abandon_cart><!-- tag of the system.xml group -->
                <abandon_cart_media_path>path</abandon_cart_media_path><!-- tag of the system.xml field -->
            </abandon_cart>
        </abc_abandon>
    </default>

</config>

i have change noNamespaceSchemaLocation for both file

Please refer this :- Magento 2: Default Values for `system.xml` Configuration

  • This is only working for enable disable fields not for text fields – Shomita Jan 9 at 9:51
1

Please correct below code in your config.xml

app\code\Abc\Abandon\etc\config.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Store:etc/config.xsd">
    <default>
        <abc_abandon> <!-- section ID -->
            <abc_cart> <!-- group ID -->
                <abandon_cart_media_path>0</abandon_cart_media_path> <!-- field ID -->
            </abc_cart>
        </abc_abandon>
    </default>
</config>

Clear Magento cache once and check.

Hope this will help you!

  • What you have changed. code is similar – Shomita Jan 9 at 7:25
  • I think your group ID is not correct in your question. Please review that once. – Kishan Savaliya Jan 9 at 7:26
  • This is typo mistake. But it's correct in my code – Shomita Jan 9 at 7:27
  • updated group id – Shomita Jan 9 at 7:28

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.