Sooner or later you’ll run into a case where you need REST API for your online store and you found that Magento 1 does not support all Magento 1 REST API. What should you do now? How can you write REST API for your online store as per client need?
The Magento 1 REST API allows you to use various data entities such as customer, catalog and order information to third party applications. I’d like to share it with you a quick, simple, well written and easy solution which will explain how can you retrieve the data with the help of REST API.
When working with Magento 1 REST API, for some specific reasons a developer wants to access a list of countries with regions to display the countries list on the mobile app. We first needed to access a collection of countries in Magento 1, then we will get the region based on the country. Below is some sample code that we can use to access country names and codes in Magento.
Magento 1 collection of all countries
Magento 1 has a model which provides all the countries list and below is the code for getting the collection of all countries.
public function getCountryCollection(){ //get the all countries $countryCollection = Mage::getModel('directory/country_api')->items(); }
Magento 1 collection of regions related to specific country
Magento 1 has a predefined model which takes countryId and return all the available region, below is the code for getting the available region based on the countryId.public function getRegionByCountryId(){ // get region based on countries $regions = Mage::getModel('directory/region_api')->items($countryId); }
Get All countries Collection with their Region
Magento 1 has a predefined way to get countries and available region of the specified countries but does not provide the all countries list with the available region. I have used both above ways to create a single REST API and get all the countries along with the regions. Below is the complete code.public function getCountriesWithRegions(){ //get all countries $countryCollection = Mage::getModel('directory/country_api')->items(); $countries = array(); foreach($countryCollection as $country) { // get region based on countries $regions = Mage::getModel('directory/region_api')->items($country['country_id']); $availableRegion = array(); foreach ($regions as $region){ $temData = array( 'id' => $region['region_id'], 'code' => $region['code'], 'name' => $region['name'] ); array_push($availableRegion, $temData); }// creating array which contains country with available region
$data = array(
'id' => $country['country_id'],
'two_letter_abbreviation' => $country['iso2_code'],
'three_letter_abbreviation' => $country['iso3_code'],
'full_name_english' => $country['name'],
'available_regions' => $availableRegion
);
array_push($countries, $data);
}// returning response
return $countries;
}
Conclusion
This is the era of eCommerce with the Mobile app as peoples are spending time on their mobile. The user has various choices to purchase an item from the web that’s why your eCommerce website should remain competitive in your business. The mobile app is a new way, which turns your Magento 1 website into a fully functional eCommerce store with everything user needs.On this tutorial, we have discussed how to get all the country list along with the region in Magento 1 which you can use to develop your Magento 1 Mobile App.
References
Further reading
- Magento: Rest Api for Product Details Promatically
- Magento: Token Base Rest API for authentication and authorization
- Magento: Rest Api for Product Categories programtically
Posted from my blog with : https://www.ipragmatech.com/ultimate-magento-developer-guide-country-region-collection-using-restapi-magento-1-x/
Hi! I am a robot. I just upvoted you! I found similar content that readers might be interested in:
https://www.ipragmatech.com/ultimate-magento-developer-guide-country-region-collection-using-restapi-magento-1-x/
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit