{"id":1443,"date":"2020-12-03T05:19:23","date_gmt":"2020-12-03T05:19:23","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/add-date-using-dynamic-field-in-system-xml-file-in-magento-2\/"},"modified":"2025-06-16T15:44:27","modified_gmt":"2025-06-16T10:14:27","slug":"add-date-using-dynamic-field-in-system-xml-file-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/add-date-using-dynamic-field-in-system-xml-file-in-magento-2\/","title":{"rendered":"How to Add Date Using Dynamic Field in system.xml File in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Magento 2 CMS is a widely used platform to develop online stores. Owing to the flexibility of customization, the CMS is preferred to cater to modern business requirements.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, I\u2019ve posted a similar solution that you can implement if your business requires to configure settings based on the calendar dates from the system configuration.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">For example, you may want to restrict certain dates for delivery that are national holidays, or set exclusive offers for a certain period of time.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img decoding=\"async\" src=\"https:\/\/meetanshi.com\/blog\/wp-content\/uploads\/2020\/12\/add-date-using-dynamic-field-in-Magento-2-1024x367.gif\" alt=\"add date using dynamic field in Magento 2\" class=\"wp-image-11829\"\/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">Such functionalities can be implemented easily with the programmatic solution to&nbsp;<em><strong>add date using dynamic field in system.xml in Magento 2<\/strong><\/em>&nbsp;store.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Method to Add Date Using Dynamic Field in system.xml File in Magento 2:<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">1. Create&nbsp;<em><strong>registration.php<\/strong><\/em>&nbsp;file at&nbsp;<strong><strong>app\\code\\Vendor\\Module directory<\/strong><\/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\nuse \\Magento\\Framework\\Component\\ComponentRegistrar;\n\nComponentRegistrar::register(ComponentRegistrar::MODULE, 'Vendor_Module', __DIR__);<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">2. Create&nbsp;<em><strong>module.xml<\/strong><\/em>&nbsp;file at&nbsp;<strong><strong>app\\code\\Vendor\\Module\\etc directory<\/strong><\/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\">3. Create&nbsp;<em><strong>system.xml<\/strong><\/em>&nbsp;file at&nbsp;<strong><strong>app\\code\\Vendor\\Module\\etc\\adminhtml\\system.xml<\/strong><\/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\" xsi:noNamespaceSchemaLocation=\"urn:magento:module:Magento_Config:etc\/system_file.xsd\">\n    &lt;system>\n        &lt;tab id=\"vendor\" translate=\"label\" class=\"dynamic_field\" sortOrder=\"100\">\n            &lt;label>&lt;\/label>\n        &lt;\/tab>\n        &lt;section id=\"deliverydate\" translate=\"label\" type=\"text\" sortOrder=\"320\" showInDefault=\"1\" showInWebsite=\"1\"\n                 showInStore=\"1\">\n            &lt;label>Order Delivery Date&lt;\/label>\n            &lt;tab>vendor&lt;\/tab>\n            &lt;resource>Vendor_Module::module_configuration&lt;\/resource>\n            &lt;group id=\"general\" translate=\"label\" type=\"text\" sortOrder=\"1\" showInDefault=\"1\" showInWebsite=\"1\"\n                   showInStore=\"1\">\n                &lt;label>General&lt;\/label>\n                &lt;field id=\"enable\" translate=\"label\" type=\"select\" sortOrder=\"1\" showInDefault=\"1\" showInWebsite=\"1\"\n                       showInStore=\"1\">\n                    &lt;label>Enable&lt;\/label>\n                    &lt;source_model>Magento\\Config\\Model\\Config\\Source\\Enabledisable&lt;\/source_model>\n                &lt;\/field>\n                &lt;field id=\"cutofftime\" translate=\"cut off time\" type=\"time\" sortOrder=\"4\" showInDefault=\"1\" showInWebsite=\"1\"\n                       showInStore=\"1\">\n                    &lt;label>Cut off Time&lt;\/label>\n                    &lt;comment>\n                        &lt;![CDATA[If customers place order after this time the date when orders made will be counted as the following day.]]>\n                    &lt;\/comment>\n                &lt;\/field>\n                &lt;field id=\"dynamic_field_holidays\" translate=\"label\" sortOrder=\"4\" showInDefault=\"1\" showInWebsite=\"1\" showInStore=\"1\">\n                    &lt;label>Holidays\/Exclude Days\/Blackout Days&lt;\/label>\n                    &lt;backend_model>Vendor\\Module\\Block\\Adminhtml\\Config\\Backend\\ArraySerializedHolidays&lt;\/backend_model>\n                    &lt;frontend_model>Vendor\\Module\\Block\\Adminhtml\\DynamicFieldHolidays&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\">4. Create&nbsp;<em><strong>ArraySerializedHolidays.php<\/strong><\/em>&nbsp;file at&nbsp;<strong><strong><strong><strong>Vendor\\Module\\Block\\Adminhtml\\Config\\Backend<\/strong><\/strong><\/strong><\/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 ArraySerializedHolidays extends ConfigValue\n{\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        $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        if (isset($value['__empty'])) {\n            unset($value['__empty']);\n        }\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\">5. Create&nbsp;<em><strong>DynamicFieldHolidays.php<\/strong><\/em>&nbsp;file at&nbsp;<strong><strong><strong><strong>Vendor\\Module\\Block\\Adminhtml<\/strong><\/strong><\/strong><\/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\\Backend\\Block\\Template\\Context;\nuse Magento\\Config\\Block\\System\\Config\\Form\\Field\\FieldArray\\AbstractFieldArray;\nuse Magento\\Framework\\DataObject;\nuse Magento\\Framework\\Registry;\n\nclass DynamicFieldHolidays extends AbstractFieldArray\n{\n    private $holidaysRenderer;\n    private $dateRenderer;\n\n    public function __construct(\n        Context $context,\n        Registry $coreRegistry,\n        array $data = []\n    ) {\n        $this->_coreRegistry = $coreRegistry;\n        parent::__construct($context, $data);\n    }\n\n    protected function _prepareToRender()\n    {\n\n        $this->addColumn(\n            'select_date',\n            [\n                'label' => __('Date'),\n                'id' => 'select_date',\n                'class' => 'daterecuring',\n                'style' => 'width:200px'\n            ]\n        );\n\n        $this->addColumn(\n            'date_title',\n            [\n                'label' => __('Content'),\n                'class' => 'required-entry',\n                'style' => 'width:300px',\n            ]\n        );\n\n        $this->_addAfter = false;\n        $this->_addButtonLabel = __('MoreAdd');\n    }\n\n    protected function _prepareArrayRow(DataObject $row): void\n    {\n        $options = [];\n        $row->setData('option_extra_attrs', $options);\n    }\n\n    protected function _getElementHtml(\\Magento\\Framework\\Data\\Form\\Element\\AbstractElement $element)\n    {\n        $html = parent::_getElementHtml($element);\n\n        $script = '&lt;script type=\"text\/javascript\">\n                require([\"jquery\", \"jquery\/ui\", \"mage\/calendar\"], function (jq) {\n                    jq(function(){\n                        function bindDatePicker() {\n                            setTimeout(function() {\n                                jq(\".daterecuring\").datepicker( { dateFormat: \"mm\/dd\/yy\" } );\n                            }, 50);\n                        }\n                        bindDatePicker();\n                        jq(\"button.action-add\").on(\"click\", function(e) {\n                            bindDatePicker();\n                        });\n                    });\n                });\n            &lt;\/script>';\n        $html .= $script;\n        return $html;\n    }\n}<\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">That\u2019s it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Now you know how to <a href=\"https:\/\/meetanshi.com\/blog\/add-dynamic-field-in-magento-2-admin-using-system-xml\/\" data-type=\"link\" data-id=\"https:\/\/meetanshi.com\/blog\/add-dynamic-field-in-magento-2-admin-using-system-xml\/\">Add a Dynamic Field in Magento 2<\/a> Admin Using system.xml file<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also read <a href=\"https:\/\/meetanshi.com\/blog\/implement-field-dependency-from-different-groups-in-magento-2-system-xml\/\">How to Implement Field Dependency from Different Groups in Magento 2 System.xml<\/a>\u00a0based on the selection of API providers.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Also, do share the solution with Magento Community via social media.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Thank you.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Magento 2 CMS is a widely used platform to develop online stores. Owing to the flexibility of customization, the CMS is preferred to cater to&#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-1443","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1443","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=1443"}],"version-history":[{"count":5,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1443\/revisions"}],"predecessor-version":[{"id":16926,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/1443\/revisions\/16926"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=1443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=1443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=1443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}