Add locally required data to orders using localized fields
Localized fields are additional fields that are required under certain circumstances,
depending on the shop country, destination country, and origin countries for all the
merchandise in the cart. For example, some countries require additional fields for customs information or tax identification numbers. You can retrieve the information from these fields using the LocalizedFieldConnection
connection.
This guide shows how to create and query country fields using the GraphQL Admin API.
Anchor to RequirementsRequirements
- Your app can make authenticated requests to the GraphQL Admin API.
- Your app has the
read_orders
access scope. Learn how to configure your access scopes using Shopify CLI. - You've created at least one draft order and order in your Shopify store.
Anchor to Step 1: Add a country fieldStep 1: Add a country field
You can add a country field to a draft order or an existing order using the GraphQL Admin API, or you can collect it on checkout.
In this step, you'll use a Brazilian CPF number. Visit the help docs to view the complete list of country fields that can be collected.
Anchor to Add a country field to a draft orderAdd a country field to a draft order
To add a country field to a DraftOrder
, pass the LocalizedFieldInput
object to the draftOrderCreate
mutation.
The following mutation creates a draft order with a Brazilian CPF number:
POST https://{shop}.myshopify.com/api/{api_version}/graphql.json
GraphQL mutation
Variables
JSON response
Anchor to Convert the draft order into an orderConvert the draft order into an order
Using the id
value from the response of the draftOrderCreate
mutation, use the draftOrderComplete
mutation to convert the draft order into an order.
POST https://{shop}.myshopify.com/api/{api_version}/graphql.json
GraphQL mutation
Variables
JSON response
Anchor to Add a country field to an existing orderAdd a country field to an existing order
To add a country field to an existing order, pass the LocalizedFieldInput
input object to the orderUpdate
mutation.
The following mutation updates an existing order with a Brazilian CPF number:
POST https://{shop}.myshopify.com/api/{api_version}/graphql.json
GraphQL mutation
Variables
JSON response
Anchor to Collect a country field on checkoutCollect a country field on checkout
To test this feature, you need an order containing a localized field that was collected during checkout. You can create a localized field using a developer preview store.
To collect a country field on checkout, you can set the address of your store to Brazil (since Brazilian merchants are required to collect a tax ID for government invoicing) and complete a checkout from your storefront.
-
Set the address of your store to Brazil. Remember to change your address back when you've completed the tutorial.
-
From your storefront, add a product to your cart and then complete a checkout.
-
Enter a valid Brazilian CPF number into the new CPF/CNPJ localized field in the Additional information section. If you don’t have one, then you can use
39053344705
. -
Complete the checkout.
-
Open the order in the Shopify admin and verify that the
CPF/CNPJ
value is displayed inside the customer card, in the Additional information section.
Anchor to Step 2: Retrieve the country fieldStep 2: Retrieve the country field
To retrieve a country field for an order, you can use the order
query. Pass the order ID to the order query and request the first five entries of the localizedFields
connection. Check the response to make sure that the CPF/CNPJ
field is returned.
POST https://{shop}.myshopify.com/api/{api_version}/graphql.json
GraphQL query
JSON response
Anchor to Validate a country fieldValidate a country field
You can use a checkout UI extension or Shopify Function to implement custom validation for the localized field. Learn more about building custom validation for checkout in the following tutorials:
- Create client-side validation with checkout UI extensions
- Create server-side validation with a Cart and Checkout Validation Function
Anchor to Next stepsNext steps
- Use the GraphQL Admin API to translate an online store's content.
- Use the Storefront API to retrieve translated content.
- Explore an example for retrieving a list of shop locales with the GraphQL Admin API.
- Explore examples for retrieving translatable resources with the GraphQL Admin API.
- Learn how to support multiple currencies and languages in your theme.
- Learn how to associate markets with different legal entities.