1

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

Browse other questions tagged or ask your own question.