How to Add Timezone & Locale Dropdown in Custom Form in Frontend in Magento 2
Magento 2 CMS is widely used global platform for online stores. The way we added week days list in Magento 2 for developers, likewise here we need to add timezone and locale dropdown for customers.
As Magento 2 stores serve customers from various location living in different time zones. And you are a business that offers on-call support, you may require to know the time zone of your customer’s location. Or if you have implemented shipping restrictions based on locale, you may require to know the locale information of your customer before accepting their orders.
In such scenarios, you may add timezone & locale dropdown in custom form in frontend in Magento 2 as shown below:
The timezone and locale in a custom form
Use the below code to get the customers’ time zone and locale data from the frontend as shown in the above figure.
Steps to Add Timezone & Locale Dropdown in Custom Form in Frontend in Magento 2
- Add the below code in your block file.
123456789101112131415161718192021222324252627282930313233<?phpnamespace Meetanshi\Module\Block;use Magento\Framework\Locale\ListsInterface;use Magento\Framework\View\Element\Template;class CustomForm extends Template{protected $listInterface;public function __construct(Template\Context $context,ListsInterface $listInterface,array $data = []){$this->listInterface = $listInterface;parent::__construct($context, $data);}public function getTimeZoneList(){$timezonesList = $this->listInterface->getOptionTimezones();return $timezonesList;}public function getLocaleList(){$localeList = $this->listInterface->getOptionLocales();return $localeList;}} - Add the below code in your phtml file.
12345678910111213141516171819202122232425<?php/** @var \Meetanshi\Module\Block\CustomForm $block */$timezones = $block->getTimeZoneList();$locales = $block->getLocaleList();?><?php if (sizeof($timezones) > 0): ?><div class="timezone"><label for="timezone"><?php echo __('Select Timezone'); ?></label><select name="timezone" id="timezone"><?php foreach ($timezones as $timezone): ?><option value="<?php echo $timezone['value']; ?>"><?php echo $timezone['label']; ?></option><?php endforeach; ?></select></div><?php endif; ?><?php if (sizeof($locales) > 0): ?><div class="locale"><label for="locale"><?php echo __('Select Locale'); ?></label><select name="locale" id="locale"><?php foreach ($locales as $locale): ?><option value="<?php echo $locale['value']; ?>"><?php echo $locale['label']; ?></option><?php endforeach; ?></select></div><?php endif; ?>
That’s it!
If you have any doubts about the solution, mention them in the Comments section below.
I would be happy to help.
Feel free to share the solution with Magento Community via social media.
Thank You.
Chandresh Chauhan
He has been with Meetanshi for more than three years now as a certified Magento developer. A silent guy whom you can always find solving clients' issues, is an avid reader too.
Prev
Solved: Blank Pages Issue After Compilation Command in Magento 2
How To Get All Category URLs in Magento 2
Next