Magento2

How to Create System.xml Configuration and get data from System Configuration in Magento 2?

How to Create System.xml Configuration and get data from System Configuration in Magento 2?

The system.xml is a configuration file which is used to manage the Magento system configuration. After creating this file you can check Store -> Settings -> Configuration.

File Path : app/code/V4U/Helloworld/etc/adminhtml/system.xml

Step 1 : Create system.xml file

Step 2 : You can set default value of system.xml fields

File Path : app/code/V4U/Helloworld/etc/config.xml

Step 3 : Flush the cache : Using Terminal : Run the below command in terminal

php bin/magento cache:flush or php bin/magento c:f

From Admin Panel : Admin Panel -> System -> Tools -> Cache Management

Step 4 : Get value from configuration

Way 1 : By creating helper

File Path : app/code/V4U/Helloworld/Helper/Data.php

Now create block and call helper

File Path : app/code/V4U/Helloworld/Block/Helloworld.php

Now can call the system config value in PHTML file.

File Path : app/code/V4U/Helloworld/view/frontend/templates/helloworld.phtml

Or you can call helper direct in PHTML file.

Way 2 : Using dependency

Way 3 : Using instantiating a block (in .phtml file)

Way 4 : Using ObjectManager (It’s not good practice to use directly use ObjectManager. Always use constructor via DI(Dependency injection).Here is just used for concept)

🙂 Happy Coding. Keep Liking & Sharing 💻

Tagged ,