7

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.

3

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;
    }
}
  • @Mohammed, can you please update the full code with di.xml also? – jafar pinjar Nov 26 '18 at 14:40
  • @jafarpinjar , i have updated my code, hope it will help you – Mohammad Maheboob Dec 3 '18 at 6:48
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:)

  • Where i need to create this file.? I have created the file in the vendor. But it did not work. – Sunny Rahevar Feb 23 '18 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 '18 at 20:43
  • @Dmitry, can you please update full code with di/xml also? – jafar pinjar Nov 26 '18 at 14:41
  • @Dmitry, used the plugin, its not working for me – jafar pinjar Nov 26 '18 at 15:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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