1

I need to add these 2 dropdown to the custom frontend form.

enter image description here

enter image description here

Any help would be appreciate. Thank you!

1

You can get your result by following code:

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); ?>

<?php $localeLists = $objectManager->create('Magento\Framework\Locale\ListsInterface'); ?>
<?php $timezones = $localeLists->getOptionTimezones(); ?>
<?php $locales = $localeLists->getOptionLocales(); ?>

<?php if(count($timezones) > 0): ?>
    <label><?= __('Timezone') ?></label>
    <select name="timezone" id="timezone">
        <?php foreach($timezones as $timezone): ?>
            <option value="<?= $timezone['value'] ?>"><?= $timezone['label'] ?></option>
        <?php endforeach; ?>
    </select>
<?php endif; ?>

<?php if(count($locales) > 0): ?>
    <label><?= __('Locale') ?></label>
    <select name="locale" id="locale">
        <?php foreach($locales as $locale): ?>
            <option value="<?= $locale['value'] ?>"><?= $locale['label'] ?></option>
        <?php endforeach; ?>
    </select>
<?php endif; ?>

Don't use objectmanager in your phtml file. Inject the ListsInterface class to your block construct method like below code:

public function __construct(
    ...
    \Magento\Framework\Locale\ListsInterface $localeLists,
    ...
) {
    ...
    $this->localeLists = $localeLists;
    ...
}

public function getTimezones()
{
    return $this->localeLists->getOptionTimezones();
}

public function getLocales()
{
    return $this->localeLists->getOptionLocales();
}

Now call it in phtml like below:

<?php $timezones = $block->getTimezones(); ?>
<?php $locales = $block->getLocales(); ?>

<?php if(count($timezones) > 0): ?>
    <label><?= __('Timezone') ?></label>
    <select name="timezone" id="timezone">
        <?php foreach($timezones as $timezone): ?>
            <option value="<?= $timezone['value'] ?>"><?= $timezone['label'] ?></option>
        <?php endforeach; ?>
    </select>
<?php endif; ?>

<?php if(count($locales) > 0): ?>
    <label><?= __('Locale') ?></label>
    <select name="locale" id="locale">
        <?php foreach($locales as $locale): ?>
            <option value="<?= $locale['value'] ?>"><?= $locale['label'] ?></option>
        <?php endforeach; ?>
    </select>
<?php endif; ?>

Hope this helps!

1
  • 1
    I achieve by this way <?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); ?> <?php $localeLists = $objectManager->create('\Magento\Config\Model\Config\Source\Locale\Timezone'); ?> <?php $timezones = $localeLists->toOptionArray(); ?> but thanks a lot for your answer – Ronak Jan 6 at 7:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.