Magento2 Admin Settings – How to Add a New Group to an Existing Section

adminsystem.xml

So I'm trying to add a new group called Checkout Gifts inside the section checkout in the tab sales.

I was able to add a section to an existing tab but when I tried the same approach to create a new group inside an section it didn't work and displayed errors.

The approach:

<?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>
        <group id="checkout_gifts" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
            <class>separator-top</class>
            <tab>sales</tab>
            <section>checkout</section>
            <resource>Magento_Sales::config_sales</resource>
            <label>Gift Options</label>
            <field id="gift_wrap" type="select" translate="label" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Gift Wrap</label>
                <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
            </field>
            <field id="gift_ticket" type="select" translate="label" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Gift Ticket</label>
                <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
            </field>
            <field id="gift_message" type="select" translate="label" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Gift Message</label>
                <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
            </field>
        </group>
    </system>  
</config>

The error:

1 exception(s):
Exception #0 (Magento\Framework\Exception\LocalizedException): The XML in file "/var/www/app/code/Tmg/Checkout/etc/adminhtml/system.xml" is invalid:
Element 'group': This element is not expected. Expected is one of ( tab, section, include ).
Line: 4

Verify the XML and try again.

Exception #0 (Magento\Framework\Exception\LocalizedException): The XML in file "/var/www/app/code/Tmg/Checkout/etc/adminhtml/system.xml" is invalid:
Element 'group': This element is not expected. Expected is one of ( tab, section, include ).
Line: 4

Is this possible? How can I do this?

Best Answer

You have added wrong code. Please replace it with below.

<?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="checkout">
            <tab>sales</tab>
            <resource>Magento_Checkout::checkout</resource>
            <group id="checkout_gifts" translate="label" type="text" sortOrder="10" showInDefault="1" showInWebsite="1" showInStore="1">
                <label>Gift Options</label>
                <field id="gift_wrap" type="select" translate="label" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Gift Wrap</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="gift_ticket" type="select" translate="label" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Gift Ticket</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
                <field id="gift_message" type="select" translate="label" sortOrder="1" showInDefault="1" showInWebsite="1" showInStore="1">
                    <label>Gift Message</label>
                    <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
                </field>
            </group>
        </section>
    </system>  
</config>