2

There is default API's of country listing in magento 2 i.e directoryCurrencyInformationAcquirerV1 & route URL GET /V1/directory/currency.

The output of this API.

[{
        "id": "AD",
        "two_letter_abbreviation": "AD",
        "three_letter_abbreviation": "AND",
        "full_name_locale": "Andorra",
        "full_name_english": "Andorra"
    },
    {
        "id": "AE",
        "two_letter_abbreviation": "AE",
        "three_letter_abbreviation": "ARE",
        "full_name_locale": "United Arab Emirates",
        "full_name_english": "United Arab Emirates"
    },
    {
        "id": "AF",
        "two_letter_abbreviation": "AF",
        "three_letter_abbreviation": "AFG",
        "full_name_locale": "Afghanistan",
        "full_name_english": "Afghanistan"
    },
    {
        "id": "AG",
        "two_letter_abbreviation": "AG",
        "three_letter_abbreviation": "ATG",
        "full_name_locale": "Antigua & Barbuda",
        "full_name_english": "Antigua & Barbuda"
    },
    {
        "id": "AI",
        "two_letter_abbreviation": "AI",
        "three_letter_abbreviation": "AIA",
        "full_name_locale": "Anguilla",
        "full_name_english": "Anguilla"
    },
    {
        "id": "AL",
        "two_letter_abbreviation": "AL",
        "three_letter_abbreviation": "ALB",
        "full_name_locale": "Albania",
        "full_name_english": "Albania"
    },
    {
        "id": "AM",
        "two_letter_abbreviation": "AM",
        "three_letter_abbreviation": "ARM",
        "full_name_locale": "Armenia",
        "full_name_english": "Armenia"
    },
    {
        "id": "AN",
        "two_letter_abbreviation": "AN",
        "three_letter_abbreviation": "ANT",
        "full_name_locale": null,
        "full_name_english": null
    },
    {
        "id": "AO",
        "two_letter_abbreviation": "AO",
        "three_letter_abbreviation": "AGO",
        "full_name_locale": "Angola",
        "full_name_english": "Angola"
    },
    {
        "id": "AQ",
        "two_letter_abbreviation": "AQ",
        "three_letter_abbreviation": "ATA",
        "full_name_locale": "Antarctica",
        "full_name_english": "Antarctica"
    },
    {
        "id": "AR",
        "two_letter_abbreviation": "AR",
        "three_letter_abbreviation": "ARG",
        "full_name_locale": "Argentina",
        "full_name_english": "Argentina"
    },
    {
        "id": "AS",
        "two_letter_abbreviation": "AS",
        "three_letter_abbreviation": "ASM",
        "full_name_locale": "American Samoa",
        "full_name_english": "American Samoa"
    },
    {
        "id": "AT",
        "two_letter_abbreviation": "AT",
        "three_letter_abbreviation": "AUT",
        "full_name_locale": "Austria",
        "full_name_english": "Austria",
        "available_regions": [{
                "id": "102",
                "code": "BL",
                "name": "Burgenland"
            },
            {
                "id": "99",
                "code": "KN",
                "name": "Kärnten"
            },
            {
                "id": "96",
                "code": "NO",
                "name": "Niederösterreich"
            },
            {
                "id": "97",
                "code": "OO",
                "name": "Oberösterreich"
            },
            {
                "id": "98",
                "code": "SB",
                "name": "Salzburg"
            },
            {
                "id": "100",
                "code": "ST",
                "name": "Steiermark"
            },
            {
                "id": "101",
                "code": "TI",
                "name": "Tirol"
            },
            {
                "id": "103",
                "code": "VB",
                "name": "Vorarlberg"
            },
            {
                "id": "95",
                "code": "WI",
                "name": "Wien"
            }
        ]
    }
]

And i have override this API's but unable to sort by full_name_english key. Is there any one who can sort full name english so that united arab emirates will be according to ascending order.

1

Override the country model class using the plugin.

Put the below code in your custom module di.xml

<type name="Magento\Directory\Model\CountryInformationAcquirer">
        <plugin name="CountryList" type="[vendor]\[module]\Plugin\CountryList"/>
    </type>

Create plugin folder inside [vendor]->[module] & create CountryList.php

then, Put the below code & play with $result return by default country list API.

<?php

namespace Vendorname\modulename\Plugin;

class CountryList
{
    public function afterGetCountriesInfo(\Magento\Directory\Model\CountryInformationAcquirer $subject, $result) {
        $data = [];
        foreach ($result as $key => $value) {
            $data[$key]['id'] = $value->getId();
            $data[$key]['two_letter_abbreviation'] = $value->getTwoLetterAbbreviation();
            $data[$key]['three_letter_abbreviation'] = $value->getThreeLetterAbbreviation();
            $data[$key]['full_name_locale'] = $value->getFullNameLocale();
            $data[$key]['full_name_english'] = $value->getFullNameEnglish();
            $regions = [];
            if (!empty($value->getAvailableRegions())) {
                foreach ($value->getAvailableRegions() as $keyr => $region) {
                    $data[$key]['available_regions'][$keyr]['id'] = $region->getId();
                    $data[$key]['available_regions'][$keyr]['code'] = $region->getCode();
                    $data[$key]['available_regions'][$keyr]['name'] = $region->getName();
                }
            }
        }
        usort($data, function ($item1, $item2) {
            return $item1['full_name_english'] <=> $item2['full_name_english'];
        });
        return $data;
      }
}

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.