1

I am using Magento GraphQL api in my project. To create a customer address I used createCustomerAddress mutation(createCustomerAddress).

Below is the mutation that I have called to create the customer address :

mutation createAddress {
  createCustomerAddress(
    input: {
      firstname: "test"
      lastname: "name"
      company: "networld"
      telephone: "1231231231"
      street: ["test address line 1", "test address line 2"]
      city: "Rajkot"
      region: { region:"Gujarat", region_code: "GJ" }
      postcode: "360001"
      country_code: IN
    }
  ) {
    id
    prefix
    firstname
    lastname
    middlename
    city
    company
    country_code
    default_billing
    default_shipping
    postcode
    region {
      region
      region_code
    }
    street
    suffix
    telephone
    vat_id
  }
}

This is working properly and returning me the result as below :

{
  "data": {
    "createCustomerAddress": {
      "id": 44,
      "prefix": null,
      "firstname": "test",
      "lastname": "name",
      "middlename": null,
      "city": "Rajkot",
      "company": "networld",
      "country_code": "IN",
      "default_billing": false,
      "default_shipping": false,
      "postcode": "360001",
      "region": {
        "region": "Gujarat",
        "region_code": "GJ"
      },
      "street": [
        "test address line 1",
        "test address line 2"
      ],
      "suffix": null,
      "telephone": "1231231231",
      "vat_id": null
    }
  }
}

But, now when I query to get the customer address, it returning wrong region_code.

Here is the query I written to get the customer address :

query{
  customer{
    addresses{
      id
      firstname
      lastname
      street
      city
      region{
        region
        region_code
      }
      country_code
      postcode
      telephone
    }
  }
}

Result :

{
  "data": {
    "customer": {
      "addresses": [
        {
          "id": 44,
          "firstname": "test",
          "lastname": "name",
          "street": [
            "test address line 1",
            "test address line 2"
          ],
          "city": "Rajkot",
          "region": {
            "region": "Gujarat",
            "region_code": "Gujarat"
          },
          "country_code": "IN",
          "postcode": "360001",
          "telephone": "1231231231"
        }
      ]
    }
  }
}

As you can see, region_code in this query result and region_code in mutation result was different. Query not returning region_code that generated from the mutation. Mutation generated region_code was GJ and query returned region_code was Gujarat.

Can anyone help me why this is happening ? How to solve it ?

2

I have checked your query. It seems like you will have pass one additional field while calling "createCustomerAddress" mutation within "region" array. You will have to pass "region_id" along with "region" and "region_code" in that array.

Like this :

region: {
      region: "Georgia"
      region_code: "GA"
      region_id:19
    } 

If you pass like this then you will get the exact region code as "GA" in customer query which you call after this. I have already done this practise in my Altair. It should work fine for you as well.

FYI : My magento2 version is 2.3.3

Thanks,

| improve this answer | |
  • Ok, but in document it displaying that region_id is deprecated, "region_id is deprecated. Region ID is excessive on storefront and region code should suffice for all scenarios". – Kishan Bharda Aug 27 at 13:34
  • And, no problem if it is deprecated, but from where I can get region_id for perticular region ? – Kishan Bharda Aug 27 at 13:35
  • @KishanBharda, yes it might be deprecated and that's why I have posted my magento version along with the answer. If you are ready to accept this and want to pass region_id then you can find it from the region dropdown on checkout page. In region dropdown you should have value and label fields. In value the region id will be there and in label region label will be there. If you want to check in database then you will find it in this table "directory_country_region". Hope this help to you. Thanks! – Pratik Navapara Aug 27 at 13:38
  • I am also on version 2.3.5. And I am using this api to develop a mobile app of a magento site. So, there is an issue to get region_id. However there is an api to get countries and region which returning region_id also. So, that can be helpful. But still I have to use deprecated field to solve this. Any solution without using deprecated field? – Kishan Bharda Aug 27 at 13:50
  • I don't think that you will face any problem if you use this because magento has also listed it on devdocs devdocs.magento.com/guides/v2.4/graphql/mutations/… for magento version 2.4. Still, If I will find any other alternative solution then I will let you know. Thanks! – Pratik Navapara Aug 27 at 13:55

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.