🐙

英語表記の都道府県を日本語表記に変換するJavaScript(Shopify API)

2022/10/27に公開約7,000字

Shopify APIで注文者情報を取得したところ、都道府県のところだけ英語表記となっていましたので、日本語表記に変換するコードを紹介します。

このコードは、web/helpers/の中に置きました。

js

import { Shopify } from "@shopify/shopify-api";

export default async function orderResponse(session) {
  const client = new Shopify.Clients.Graphql(session.shop, session.accessToken);

  const orderData = await client.query({
    data: `query {
      orders(first: 100, query: "status:open") {
        edges {
          node {
            id
            shippingAddress {
                address1
                address2
                city
                company
                country
                firstName
                lastName
                name
                phone
                province
                provinceCode
                zip
            }
          }
        }
      }
    }`,
  });

  const orderCount = Object.keys(orderData.body.data.orders.edges).length;

  const orderArr = new Array();

  for (let i = 0; i < orderCount; i++){
  
    // 都道府県が英語で返ってくるので日本語に変換
    const ProvinceCode = orderData.body.data.orders.edges[i].node.shippingAddress.provinceCode;
    switch (ProvinceCode) {
      case 'JP-01':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '北海道';
        break;
      case 'JP-02':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '青森県';
        break;
      case 'JP-03':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '岩手県';
        break;
      case 'JP-04':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '宮城県';
        break;
      case 'JP-05':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '秋田県';
        break;
      case 'JP-06':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '山形県';
        break;
      case 'JP-07':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '福島県';
        break;
      case 'JP-08':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '茨城県';
        break;
      case 'JP-09':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '栃木県';
        break;
      case 'JP-10':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '群馬県';
        break;
      case 'JP-11':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '埼玉県';
        break;
      case 'JP-12':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '千葉県';
        break;
      case 'JP-13':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '東京都';
        break;
      case 'JP-14':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '神奈川県';
        break;
      case 'JP-15':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '新潟県';
        break;
      case 'JP-16':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '富山県';
        break;
      case 'JP-17':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '石川県';
        break;
      case 'JP-18':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '福井県';
        break;
      case 'JP-19':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '山梨県';
        break;
      case 'JP-20':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '長野県';
        break;
      case 'JP-21':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '岐阜県';
        break;
      case 'JP-22':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '静岡県';
        break;
      case 'JP-23':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '愛知県';
        break;
      case 'JP-24':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '三重県';
        break;
      case 'JP-25':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '滋賀県';
        break;
      case 'JP-26':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '京都府';
        break;
      case 'JP-27':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '大阪府';
        break;
      case 'JP-28':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '兵庫県';
        break;
      case 'JP-29':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '奈良県';
        break;
      case 'JP-30':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '和歌山県';
        break;
      case 'JP-31':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '鳥取県';
        break;
      case 'JP-32':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '島根県';
        break;
      case 'JP-33':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '岡山県';
        break;
      case 'JP-34':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '広島県';
        break;
      case 'JP-35':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '山口県';
        break;
      case 'JP-36':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '徳島県';
        break;
      case 'JP-37':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '香川県';
        break;
      case 'JP-38':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '愛媛県';
        break;
      case 'JP-39':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '高知県';
        break;
      case 'JP-40':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '福岡県';
        break;
      case 'JP-41':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '佐賀県';
        break;
      case 'JP-42':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '長崎県';
        break;
      case 'JP-43':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '熊本県';
        break;
      case 'JP-44':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '大分県';
        break;
      case 'JP-45':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '宮崎県';
        break;
      case 'JP-46':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '鹿児島県';
        break;
      case 'JP-47':
        orderData.body.data.orders.edges[i].node.shippingAddress.province = '沖縄県';
        break;
      default:
        console.log('Prefecture code Not applicable');
    }

    // IDに付いている余計なURLを削除
    orderData.body.data.orders.edges[i].node.id = orderData.body.data.orders.edges[i].node.id.replace('gid://shopify/Order/', '');

    // APIから取得したオブジェクトデータを連想配列に格納し直す
    orderArr[i] = orderData.body.data.orders.edges[i].node;
  }

  return orderArr;

}

Discussion

ログインするとコメントできます