3

To generate country drop down list Mage::getBlockSingleton('directory/data')->getCountryHtmlSelect() method is responsible but generated drop down has blank entry at the top along with all other countries.

How to remove this blank entry from country drop down list?

1
  • 2
    I think this is a bad idea, if you have more than one entry. If this os the vase and the user forgets to choose one, the country is wrong (in most cases) Dec 27, 2013 at 8:03

3 Answers 3

5

This might be late but I think very short & sweet solution :-

The file is this -

app/code/core/Mage/Directory/Block/Data.php

and the function is this - getCountryHtmlSelect

If you want to remove the very top blank entry from country drop down....just put false in toOptionArray(false);

Don't forget that you put this file in app/code/local/Mage this is the good practice to keep the core file untouched :)

Thanks :)

0
3
<?php
   $countryList = Mage::getResourceModel('directory/country_collection')
                ->loadData()
                ->toOptionArray(false);
?>
   <select class="required-entry" id="county" name="county">         
     <?php foreach ($countryList as $key => $value) {?>
     <option value="<?php echo $value['label']; ?>"><?php echo $value['label']; ?></option>
     <?php } ?>
   </select>

You can try above code to access the countries list it's working very fine.

0

For Magento 2 this issue fixes via little plugin:

<?php

namespace Vendor\Module\Plugin\ResourceModel\Country;

/**
 * Plugin for processing incoming arguments of the method that leading to displaying additional empty dropdown.
 */
class Collection
{
    /**
     * Arguments processing.
     *
     * @param \Magento\Directory\Model\ResourceModel\Country\Collection $subject
     * @param bool $emptyLabel
     *
     * @return bool|array
     */
    public function beforeToOptionArray(
        \Magento\Directory\Model\ResourceModel\Country\Collection $subject,
        $emptyLabel = null
    ) {
        return is_null($emptyLabel) ? [''] : null;
    }
}

enjoy:)

4
  • Q was tagged with Magento 1 ...
    – sv3n
    Oct 20, 2017 at 16:53
  • hi @Dmitry, Its not working for me, pls provide full code, with di.xml Nov 27, 2018 at 6:10
  • <?xml version="1.0"?> <config xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Directory\Model\ResourceModel\Country\Collection"> <plugin name="update_country_empty_option" type="Vendor\Module\Plugin\ResourceModel\Country\Collection" /> </type> </config>
    – Dmitry
    Feb 10, 2019 at 21:13
  • @jafarpinjar you should update "Vendor\Module" with your namespaces.
    – Dmitry
    Feb 10, 2019 at 21:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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