1

This question already has an answer here:

I have created a file for displaying all countries, but it shows We're sorry, an error has occurred while generating this email.

Please help me to fix the issue. Any help will be appreciated.

app\design\frontend\Vendor\Module\Magento_Cms\templates\countryselect.ptml

<?php
$_objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$countryHelper = $this->_objectManager->get('Magento\Directory\Model\Config\Source\Country'); 
$countryFactory = $this->_objectManager->get('Magento\Directory\Model\CountryFactory');

$countries = $countryHelper->toOptionArray(); //Load an array of countries

    foreach ( $countries as $countryKey => $country ) {

        if ( $country['value'] != '' ) { //Ignore the first (empty) value

            $stateArray = $countryFactory->create()->setId(
                $country['value']
            )->getLoadedRegionCollection()->toOptionArray(); //Get all regions for the given ISO country code

            if ( count($stateArray) > 0 ) { //Again ignore empty values
                $countries[$countryKey]['states'] = $stateArray;
            }

        }
    }

var_dump($countries);
?>

enter image description here

marked as duplicate by 7ochem, Keyul Shah, Rakesh Jesadiya, Murtuza Zabuawala, Qaisar Satti Mar 24 '17 at 9:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Browse other questions tagged or ask your own question.