{"id":1408,"date":"2020-11-19T07:00:36","date_gmt":"2020-11-19T07:00:36","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/add-dynamic-field-in-magento-2-admin-using-system-xml\/"},"modified":"2025-05-21T17:42:34","modified_gmt":"2025-05-21T12:12:34","slug":"add-dynamic-field-in-magento-2-admin-using-system-xml","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-dynamic-field-in-magento-2-admin-using-system-xml\/","title":{"rendered":"How to Add Dynamic Field in Magento 2 Admin Using system.xml"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This post offers a programmatic solution to&nbsp;add a dynamic field in Magento 2 admin using system.xml file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">With the increasing use of E-commerce and business requirements that follows, a store owner may want to set dynamic values of a field based on certain conditions.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, the value of the order amount can change dynamically at an instance of time for different customer groups.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">To configure such fields in Magento 2 admin panel, follow the below steps:<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Solution to Add a Dynamic Field in Magento 2 Admin Using system.xml file:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Here, I have added Order Amount as a dynamic product.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If a merchant wishes to set the threshold based on the customer group, a merchant can easily set up using the Order Amount dynamic field.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-large\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/11\/Add-a-Dynamic-Field-in-Magento-2-Add-a-Dynamic-Field-in-Magento-2-Admin-Using-system.xml-file-1024x500.png\" alt=\"Add a Dynamic Field in Magento 2 Add a Dynamic Field in Magento 2 Admin Using system.xml file\" class=\"wp-image-11529\"\/><\/figure>\n<\/div>\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<strong>registration.php<\/strong>&nbsp;file at&nbsp;<strong><em><strong><em>app\\code\\Vendor\\Module directory<\/em><\/strong><\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nuse \\Magento\\Framework\\Component\\ComponentRegistrar;\n\nComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_Module', __DIR__);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<strong>module.xml<\/strong>&nbsp;file at&nbsp;<strong><em><strong><em>app\\code\\Vendor\\Module\\etc directory<\/em><\/strong><\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:framework:Module\/etc\/module.xsd\">\n    &lt;module name=\"Vendor_Module\" setup_version=\"1.0.0\"\/>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<strong>system.xml<\/strong>&nbsp;file at&nbsp;<strong><em><strong><em>app\\code\\Vendor\\Module\\etc\\adminhtml directory<\/em><\/strong><\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?xml version=\"1.0\"?>\n&lt;config xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n        xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Config:etc\/system_file.xsd\">\n    &lt;system>\n        &lt;tab id=\"extension_name\" translate=\"label\" class=\"extension_name\" sortOrder=\"100\">\n            &lt;label>&lt;\/label>\n        &lt;\/tab>\n        &lt;section id=\"section_id\" translate=\"label\" type=\"text\" sortOrder=\"320\" showInDefault=\"1\" showInWebsite=\"1\"\n                 showInStore=\"1\">\n            &lt;label>Example&lt;\/label>\n            &lt;tab>extension_name&lt;\/tab>\n            &lt;resource>Vendor_Module::module_name&lt;\/resource>\n            &lt;group id=\"general\" translate=\"label\" type=\"text\" sortOrder=\"10\" showInDefault=\"1\" showInWebsite=\"1\"\n                   showInStore=\"1\">\n                &lt;label>Configuration&lt;\/label>\n                &lt;field id=\"enable\" translate=\"label\" type=\"select\" sortOrder=\"1\" showInDefault=\"1\" showInWebsite=\"0\"\n                       showInStore=\"0\">\n                    &lt;label>Order Amount for Customer Group&lt;\/label>\n                    &lt;source_model>Magento\\Config\\Model\\Config\\Source\\Enabledisable&lt;\/source_model>\n                &lt;\/field>\n                &lt;field id=\"dynamic_field\" translate=\"label\" sortOrder=\"10\" showInDefault=\"1\" showInWebsite=\"1\"\n                       showInStore=\"1\">\n                    &lt;label>Order Amount&lt;\/label>\n                    &lt;backend_model>Vendor\\Module\\Block\\Adminhtml\\Config\\Backend\\ArraySerialized&lt;\/backend_model>\n                    &lt;frontend_model>Vendor\\Module\\Block\\Adminhtml\\DynamicField&lt;\/frontend_model>\n                &lt;\/field>\n            &lt;\/group>\n        &lt;\/section>\n    &lt;\/system>\n&lt;\/config><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<strong>ArraySerialized.php<\/strong>&nbsp;file at&nbsp;<strong><em><strong><em>app\\code\\Vendor\\Module\\Block\\Adminhtml\\Config\\Backend directory<\/em><\/strong><\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Block\\Adminhtml\\Config\\Backend;\n\nuse Magento\\Framework\\App\\Cache\\TypeListInterface;\nuse Magento\\Framework\\App\\Config\\ScopeConfigInterface;\nuse Magento\\Framework\\App\\Config\\Value as ConfigValue;\nuse Magento\\Framework\\Data\\Collection\\AbstractDb;\nuse Magento\\Framework\\Model\\Context;\nuse Magento\\Framework\\Model\\ResourceModel\\AbstractResource;\nuse Magento\\Framework\\Registry;\nuse Magento\\Framework\\Serialize\\SerializerInterface;\n\nclass ArraySerialized extends ConfigValue\n{\n    protected $serializer;\n\n    public function __construct(\n        SerializerInterface $serializer,\n        Context $context,\n        Registry $registry,\n        ScopeConfigInterface $config,\n        TypeListInterface $cacheTypeList,\n        AbstractResource $resource = null,\n        AbstractDb $resourceCollection = null,\n        array $data = []\n    )\n    {\n        $this->serializer = $serializer;\n        parent::__construct($context, $registry, $config, $cacheTypeList, $resource, $resourceCollection, $data);\n    }\n\n    public function beforeSave()\n    {\n        $value = $this->getValue();\n        unset($value['__empty']);\n        $encodedValue = $this->serializer->serialize($value);\n        $this->setValue($encodedValue);\n    }\n\n    protected function _afterLoad()\n    {\n        $value = $this->getValue();\n        if ($value) {\n            $decodedValue = $this->serializer->unserialize($value);\n            $this->setValue($decodedValue);\n        }\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<strong>DynamicField.php<\/strong>&nbsp;file at<strong><em>&nbsp;<strong><em>app\\code\\Vendor\\Module\\Block\\Adminhtml directory<\/em><\/strong><\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Block\\Adminhtml;\n\nuse Magento\\Config\\Block\\System\\Config\\Form\\Field\\FieldArray\\AbstractFieldArray;\nuse Magento\\Framework\\DataObject;\nuse Vendor\\Module\\Block\\Adminhtml\\Form\\Field\\CustomColumn;\n\nclass DynamicField extends AbstractFieldArray\n{\n    private $dropdownRenderer;\n\n    protected function _prepareToRender()\n    {\n        $this->addColumn(\n            'attribute_name',\n            [\n                'label' => __('Customer Group'),\n                'renderer' => $this->getDropdownRenderer(),\n            ]\n        );\n        $this->addColumn(\n            'dropdown_field',\n            [\n                'label' => __('Purchaseover'),\n                'class' => 'required-entry',\n            ]\n        );\n        $this->_addAfter = false;\n        $this->_addButtonLabel = __('Add');\n    }\n\n    protected function _prepareArrayRow(DataObject $row)\n    {\n        $options = [];\n        $dropdownField = $row->getDropdownField();\n        if ($dropdownField !== null) {\n            $options['option_' . $this->getDropdownRenderer()->calcOptionHash($dropdownField)] = 'selected=\"selected\"';\n        }\n        $row->setData('attributes', $options);\n    }\n\n    private function getDropdownRenderer()\n    {\n        if (!$this->dropdownRenderer) {\n            $this->dropdownRenderer = $this->getLayout()->createBlock(\n                CustomColumn::class,\n                '',\n                ['data' => ['is_render_to_js_template' => true]]);\n        }\n        return $this->dropdownRenderer;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create&nbsp;<strong>CustomColumn.php<\/strong>&nbsp;file at&nbsp;<strong><em><strong><em>app\\code\\Vendor\\Module\\Block\\Adminhtml\\Form\\Field directory<\/em><\/strong><\/em><\/strong><\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\nnamespace Vendor\\Module\\Block\\Adminhtml\\Form\\Field;\n\nuse Magento\\Framework\\View\\Element\\Html\\Select;\n\nuse Magento\\Customer\\Model\\ResourceModel\\Group\\Collection;\nuse Magento\\Backend\\Block\\Template\\Context;\nuse Magento\\Customer\\Model\\Customer\\Attribute\\Source\\GroupSourceLoggedInOnlyInterface;\nuse Magento\\Framework\\App\\ObjectManager;\n\nclass CustomColumn extends Select\n{\n    protected $groupdata;\n\n    public function __construct(Context $context, GroupSourceLoggedInOnlyInterface $groupdata = null, array $data = [])\n    {\n        $this->groupdata = $groupdata\n            ?: ObjectManager::getInstance()->get(GroupSourceLoggedInOnlyInterface::class);\n        parent::__construct($context, $data);\n    }\n\n    public function setInputName($value)\n    {\n        return $this->setName($value);\n    }\n\n    public function setInputId($value)\n    {\n        return $this->setId($value);\n    }\n\n    public function _toHtml()\n    {\n        if (!$this->getOptions()) {\n            $this->setOptions($this->getSourceOptions());\n        }\n        return parent::_toHtml();\n    }\n\n    private function getSourceOptions()\n    {\n        $customerGroups = $this->groupdata->toOptionArray();\n        return $customerGroups;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">These are the six steps you need to implement to add a dynamic field in Magento 2 admin using system.xml. You can also&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/implement-field-dependency-from-different-groups-in-magento-2-system-xml\/\">implement field dependency from different groups in Magento 2 system.xml<\/a>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Help someone by sharing this post to Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post offers a programmatic solution to&nbsp;add a dynamic field in Magento 2 admin using system.xml file. With the increasing use of E-commerce and business&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-1408","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1408","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=1408"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1408\/revisions"}],"predecessor-version":[{"id":13929,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1408\/revisions\/13929"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1408"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1408"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1408"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}