Magento 2 : ifconfig in xml for module enable/disable

Magento 2 : ifconfig in xml for module enable/disable

July 17, 2026 · Illustrated author avatarBy Stefan Bogdanescu
Magento 2 : ifconfig in xml for module enable/disable

Magento 2: Conditional Module Enabling/Disabling Using ifconfig in XML

Hi there! Let’s dive into a common scenario where you want to enable or disable a Magento module based on configuration settings. In your case, you’re already using the ifconfig condition in an XML file (checkout_cart_index.xml) to control the visibility of a block based on a configuration value.

Here’s a breakdown of your current setup and how to extend this approach:

Understanding ifconfig

The ifconfig condition allows you to specify whether a certain element should be included or excluded from the layout based on the value of a configuration path. If the configuration value matches the specified condition, the XML elements are included.

In your case:

<block class="Mageniks\Test\Block\Test" before="-"
       ifconfig="mageniks/general/active"
       name="displaytest"
       template="Mageniks_Test::cart.phtml">
</block>

This block will only be displayed when the value of mageniks/general/active is set to “Yes”.

Expanding Conditional Logic

You mentioned that you’d like to add similar conditional logic to the arguments or item tags in your XML file. To do this, let’s look at a modified version of your code:

<referenceBlock name="checkout.cart.totals">
    <arguments>
        <argument name="jsLayout" xsi:type="array">
            <item name="components" xsi:type="array">
                <item name="block-totals" xsi:type="array">
                    <item name="children" xsi:type="array">

                        <item name="fee"
                              xsi:type="array"
                              ifconfig="mageniks/general/active"
                              remove="true">

                            <!-- Other XML elements here -->

                        </item>

                    </item>
                </item>
            </item>
        </argument>
    </arguments>
</referenceBlock>

In this updated version, I’ve added the ifconfig attribute to the item tag for the “fee” component. This ensures that the entire fee item is only included in the layout if the configuration value matches the specified condition.

Best Practices

  1. Naming Conventions: Try to use a consistent naming convention for your configuration paths and module names. This makes it easier to manage modules and prevents conflicts.

  2. Module-Level Configuration: If you’re working with multiple modules, consider using separate configuration paths for each module to avoid collisions.

  3. Documentation: Make sure to document your configuration paths clearly in the code and any relevant documentation files for future reference.

Conclusion

By leveraging the power of ifconfig conditions in Magento’s XML layout files, you can create dynamic and flexible layouts that adapt based on your store’s needs. Whether you’re working with blocks or arguments, this approach allows you to enable and disable modules gracefully without requiring a full module uninstall or enable.

Remember, keeping your configuration paths clear and consistent is key to maintaining a clean and manageable codebase.

Happy coding! If you need any more help, don’t hesitate to reach out. You can also explore our blog at [https://laravelcompany.com] for more Magento 2 tips and tricks.

Magento 2 Checkout Extensions Themes

Our network