Magento – magento 2 add field system config

magento2system-configsystem.xml

I am new in Magento 2. I want to add text field in Store > Configuration which store HTML code.

This HTML code will display in Frontend.

eg. I want to add note for custom shipping method this note field contain HTML code with proper HTML tag. This note is display after Shipping method in frontend.
If I add <h3> Hello </h3> than it display proper code with effect of HTML tag. Not Display like <h3> Hello </h3> in frontend.

Following system.xml

        <group id="flatrate1" translate="label" type="text" sortOrder="0" showInDefault="1" showInWebsite="1" showInStore="1">
              <label>Flat Rate 1</label>
              <field id="active" translate="label" type="select" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="0">
                  <label>Flat Rate 1</label>
                  <source_model>Namespace\Config\Model\Config\Source\Enabledisable</source_model>
              </field>
              <field id="name" translate="label" type="text" sortOrder="2" showInDefault="1" showInWebsite="1" showInStore="1">
                  <label>Method Name</label>
                  <depends><field id="active">1</field></depends>
              </field>
              <field id="title" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
                  <label>Title</label>
                  <depends><field id="active">1</field></depends>
              </field>
              <field id="type" translate="label" type="select" sortOrder="4" showInDefault="1" showInWebsite="1" showInStore="0">
                  <label>Type</label>
                  <source_model>Namespace\Multiflatrate\Model\Config\Source\Method</source_model>
                  <depends><field id="active">1</field></depends>
              </field>
              <field id="price" translate="label" type="text" sortOrder="5" showInDefault="1" showInWebsite="1" showInStore="0">
                  <label>Price</label>
                  <validate>validate-number validate-zero-or-greater</validate>
                  <depends><field id="active">1</field></depends>
              </field>
              <field id="handling_type" translate="label" type="select" sortOrder="6" showInDefault="1" showInWebsite="1" showInStore="0">
                  <label>Calculate Handling Fee</label>
                  <source_model>Namespace\Multiflatrate\Model\Config\Source\HandlingType</source_model>
                  <depends><field id="active">1</field></depends>
              </field>
              <field id="handling_fee" translate="label" type="text" sortOrder="7" showInDefault="1" showInWebsite="1" showInStore="0">
                  <label>Handling Fee</label>
                  <validate>validate-number validate-zero-or-greater</validate>
                  <depends><field id="active">1</field></depends>
              </field>
              <field id="sallowspecific" translate="label" type="select" sortOrder="8" showInDefault="1" showInWebsite="1" showInStore="0">
                  <label>Ship to Applicable Countries</label>
                  <frontend_class>shipping-applicable-country</frontend_class>
                  <source_model>Magento\Shipping\Model\Config\Source\Allspecificcountries</source_model>
                  <depends><field id="active">1</field></depends>
              </field>
              <field id="specificcountry" translate="label" type="multiselect" sortOrder="9" showInDefault="1" showInWebsite="1" showInStore="0">
                  <label>Ship to Specific Countries</label>
                  <source_model>Magento\Directory\Model\Config\Source\Country</source_model>
                  <can_be_empty>1</can_be_empty>
                  <depends><field id="active">1</field></depends>
              </field>
              <field id="showmethod" translate="label" type="select" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="0">
                  <label>Show Method if Not Applicable</label>
                  <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                  <depends><field id="active">1</field></depends>
              </field>
              <field id="specificerrmsg" translate="label" type="textarea" sortOrder="11" showInDefault="1" showInWebsite="1" showInStore="1">
                  <label>Displayed Error Message</label>
                  <depends><field id="active">1</field></depends>
              </field>
              <field id="max_amount" translate="label" type="text" sortOrder="13" showInDefault="1" showInWebsite="1" showInStore="0">
                  <label>Maximum Order Amount For Free Shipping</label>
                  <validate>validate-number validate-zero-or-greater</validate>
                  <depends><field id="active">1</field></depends>
              </field>
              <field id="min_amount" translate="label" type="text" sortOrder="14" showInDefault="1" showInWebsite="1" showInStore="0">
                  <label>Minimum Order Amount For Free Shipping</label>
                  <validate>validate-number validate-zero-or-greater</validate>
                  <depends><field id="active">1</field></depends>
              </field>
              <field id="sort_order" translate="label" type="HTML" sortOrder="100" showInDefault="15" showInWebsite="1" showInStore="0">
                  <label>Sort Order</label>
                  <depends><field id="active">1</field></depends>
              </field>

              <field id="display_text" translate="label" type="text" sortOrder="101" showInDefault="1" showInWebsite="0" showInStore="0">
                  <label>Display Text</label>
                  <comment>This text will display on the frontend.</comment>
              </field>

          </group>

Best Answer

Just add this code :

 <field id="shipnote" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
                  <label>Ship note</label>
                  <comment>This text will display on the frontend.</comment>
</field>

After this code:

   <field id="title" translate="label" type="text" sortOrder="3" showInDefault="1" showInWebsite="1" showInStore="1">
                  <label>Title</label>
                  <depends><field id="active">1</field></depends>
              </field>

For get the config text use below code or click on this link.

$this->scopeConfig->getValue('section_id/group_id/field_id', \Magento\Store\Model\ScopeInterface::SCOPE_STORE);

For more detail see here and Here

Related Topic