문제

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!

도움이 되었습니까?

해결책

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!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 magento.stackexchange
scroll top