Country and Province ISO Codes

Jimmy_Schwieter
Tourist
5 0 5
In the Api documentation, country codes conform to the ISO 3166-1 alpha-2 iso standard, but there is no mention of what iso standard is used for the provinces. Does anyone know what iso standard is used for provinces? Also there is a partial listing of what countries have province codes. Is there a complete of the countries that have provinces?
0 Likes
Carted_-_Brisba
Tourist
7 0 3

You can find out which countries have provinces by going to ?????.myshopify.com/services/countries.js it's not the best data to work with but at least it gives you a view of what is what.

yasso1am
New Member
1 0 0

If comfortable with javascript, here are the steps I used to get JUST the countries that have provinces associated with them into a workable format for my purposes:

1. Go to [your-domain].myshopify.com/services/countries.js

  • This will be a big json blob of every single country accessible to your store, as well as the provinces

2. Copy the data from above into a new file. Delete the initial "var countries =" so that the file just contains the pure JSON.

3. Create another file called countryExport.js in the same directory as the previous file.

4. Run the following command: npm i csv-writer

5. Paste the following code into the file:

const countries = require('./countries.json');
const createCsvWriter = require('csv-writer').createObjectCsvWriter;


const run = () => {
  const csvWriter = createCsvWriter({
    path: './countries.csv',
    header: [
        {id: 'country', title: 'COUNTRY'},
        {id: 'state', title: 'STATE'},
        {id: 'code', title: 'CODE'},
    ]
  });
  
  const records = [];
  
  Object.keys(countries).map( country => {
    const provinces = countries[country].province_codes;
    if (provinces != null){
      Object.keys(provinces).map( province => {
        const record = {
          country,
          state: province,
          code: provinces[province]
        }
        records.push(record);
      })
    }
  })
  
  csvWriter.writeRecords(records).then( () => {
    console.log('...done');
  }).catch( err => {
    console.log('ERROR');
    console.log(err);
  })
}

run();

6. In your terminal, navigate to the directory and enter the following command: node countriesExport

This will create a csv in the same directory of every country/province/province code available to your Shopify store.  You can modify the column titles, or include other properties from the provinces. This is just a simple example that I hope is helpful to someone.

0 Likes