9

We have configured Allow Countries to Canada in admin. On fronted, by default the checkout page country field is loading with Canada, but if we click on country drop down, empty drop-down fields are displaying.

Reference code.

<option value=""> </option>
<option data-title="" value=""></option>
<option data-title="Canada" value="CA">Canada</option>

Reference image. enter image description here

Can any one help us on restricting country field to only one country instead of displaying empty or please select value options, so that allowed country can be one country by default and country field is non-select.

2 Answers 2

5

You can create the after plugin , this logic is work fine for me , to remove blank option from the country dropdown

1) create di.xml file in app/code/Mymodule/Custom/etc/frontend/di.xml

<?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
        <type name="Magento\Directory\Model\ResourceModel\Country\Collection">
            <plugin name="remove-empty-space" 
                type="Mymodule\Custom\Plugin\Country\Collection"/>
        </type>
    </config>

2 ) create plugin in the app/code/Mymodule/Custom/Custom/Plugin/Country/Collection.php

class Collection
{
    /**
     * Arguments processing.
     *
     * @param \Magento\Directory\Model\ResourceModel\Country\Collection $subject
     * @param $options
     * @return array|bool
     *
     */
    public function afterToOptionArray(
        \Magento\Directory\Model\ResourceModel\Country\Collection $subject,
        $options
    ) {
        $result = array_filter($options, function ($option) {
            return empty($option['value']) ? false : true;
        });
        return $result;
    }
}
3
  • @Mohammed, can you please update the full code with di.xml also? Nov 26, 2018 at 14:40
  • @jafarpinjar , i have updated my code, hope it will help you Dec 3, 2018 at 6:48
  • Does not recommend, as it breaks functionality in other place. Product edit page stops opening because of attribute country of manufacture.
    – zhartaunik
    Dec 27, 2019 at 12:04
1

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:)

5
  • Where i need to create this file.? I have created the file in the vendor. But it did not work. Feb 23, 2018 at 11:51
  • 1
    You should create before plugin (devdocs.magento.com/guides/v2.0/extension-dev-guide/…) it should be like new cusom module (app\code\Vendor\Module\Plugin\ResourceModel\Country)
    – Dmitry
    Feb 25, 2018 at 20:43
  • @Dmitry, can you please update full code with di/xml also? Nov 26, 2018 at 14:41
  • @Dmitry, used the plugin, its not working for me Nov 26, 2018 at 15:20
  • @Dmitry, used the plugin but it is not working. Jun 15, 2021 at 7:34

Your Answer

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

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