3

I need some help in the module Magento directory. Actually I am able to sort id,two_letter_abbreviation & three_letter_abbreviation by setOrder to the collection which is defined in the helper class which is calling from the model.

i.e below is a function in the model class

 public function getCountriesInfo()
    {
        $countriesInfo = [];
    /** @var \Magento\Store\Model\Store $store */
    $store = $this->storeManager->getStore();

    $storeLocale = $this->scopeConfig->getValue(
        'general/locale/code',
        \Magento\Store\Model\ScopeInterface::SCOPE_STORES,
        $store->getCode()
    );

    $countries = $this->directoryHelper->getCountryCollection($store);
    $regions = $this->directoryHelper->getRegionData();
    foreach ($countries as $data) {
        $countryInfo = $this->setCountryInfo($data, $regions, $storeLocale);
        $countriesInfo[] = $countryInfo;
    }

    return $countriesInfo;
}

Below is a function define in helper class.

public function getCountryCollection($store = null)
{
    if (!$this->_countryCollection->isLoaded()) {
        $this->_countryCollection->loadByStore($store);
    }
    return $this->_countryCollection;
}

I the above, i can sort by setOrder like $this->_countryCollection->setOrder('country_id,'ASC')

or by iso2_code & iso3_code. these three are the column of directory country table

My question is that how to sort full_name_english? please help me out. After that, I will override the file.

  [
        {
            "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"
        }]

Update 1: Still did not get any solution

Update 2: I will update the answer to this once I have done with that.

Pankaj Pant
  • 593
  • 2
  • 11

2 Answers2

2

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) { if (isset($item1["full_name_english"]) && isset($item1["full_name_english"]) && !empty($item1["full_name_english"]) && !empty($item2["full_name_english"])) { return strcmp($item1["full_name_english"], $item2["full_name_english"]); } }); return $data; } }

Pankaj Pant
  • 593
  • 2
  • 11
0

#Please use strcmp function to improve result

usort($countries, function ($item1, $item2) {
        if (isset($item1["full_name_english"])
            && isset($item1["full_name_english"])
            && !empty($item1["full_name_english"])
            && !empty($item2["full_name_english"])) {
            return strcmp($item1["full_name_english"], $item2["full_name_english"]);
        }
    });
shibin vr
  • 1
  • 1