How to Add Date and Time Picker in Magento Configuration
The store owner may want to add date and time picker in Magento store to provide admin with some settings related to date and time selection. There is no direct method to call and make use of. But not to worry as I have come up with a custom code that needs to be implemented directly to Add Date and Time Picker in Magento Configuration.
Steps to Add Date and Time Picker in Magento Configuration:
Step 1: Open system.xml and implement the below code.
1 2 3 4 5 6 7 8 9 |
<fromdate translate="label"> <label>Display From</label> <frontend_type>text</frontend_type> <sort_order>10</sort_order> <show_in_default>1</show_in_default> <show_in_website>1</show_in_website> <show_in_store>1</show_in_store> <frontend_model>extension/adminhtml_date</frontend_model> </fromdate> |
Step 2: Create Date.php file at Block/Adminhtml/ Directory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php class Vendor_Extension_Block_Adminhtml_Date extends Mage_Adminhtml_Block_System_Config_Form_Field { protected function _getElementHtml(Varien_Data_Form_Element_Abstract $element) { $date = new Varien_Data_Form_Element_Date(); $format = Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT); $data = array( 'name' => $element->getName(), 'html_id' => $element->getId(), 'image' => $this->getSkinUrl('images/grid-cal.gif'), ); $date->setData($data); $date->setValue($element->getValue(), $format); $date->setFormat(Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT)); $date->setForm($element->getForm()); return $date->getElementHtml(); } } |
Once the code is implemented correctly, refresh the admin configuration page and check for the output. You’ll find the date picker at the desired position.
Thanks for reading the post. I’ll be pleased to solve any doubts regarding the topic mentioned in the Comments section. Rate it with 5 stars if you liked it. Also you can import csv in Magento Configuration to provide admin with some settings related to it.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
Magento 2.2.5 Released – All You Need to Know [27 June, 2018]
How to Force Only One Product Per Order in Magento 2
Next