How to add extra customer data to Shopify orders
How to include extra customer information with a Shopify order
You might need to collect extra information from your customers, such as a birthday, pet names, or an eyeglass prescription. The best way to collect this kind of data is through customer metafields.
Metafield values cannot be created or edited directly through the frontend of a Shopify theme, so if you wish for customers to modify metafield data via the storefront you’ll need to use an app, like Customer Fields.
The Customer Fields app allows you to easily collect extra customer information in a variety of different ways, including embedded forms and a JavaScript API. Check it out with a 14-day free trial.
Once you’ve collected additional customer data, you might want to include it with any orders that customers make. Depending on whether you want the information to be included at the product or order level, you can use one of these methods:
- Line item properties: Attach the information to a specific product in the order.
- Cart attributes: Attach the information to the entire order.
The following examples show how to use each method by editing theme code.
Note: The specific metafield namespaces, types, and keys for your store may vary. For the purpose of this article we’re going to assume you’re using the Customer Fields app which uses
customer_fields
as the namespace, with data types and keys that correlate to data columns.
Line item properties
Imagine you have a pet supply business where customers can save some information about their pets to their account. The pet information is saved as a json
type customer metafield with the following namespace and key:
- namespace:
customer_fields
- key:
pets
The value of the metafield is in the following format:
{
{
"name": "Ziggy",
"type": "dog",
"breed": "Husky",
...
},
...
}
If you want to allow customers to choose which pet a product is for when adding it to the cart, then you can add the following code snippet inside the product form.
{% if customer and customer.metafields.customer_fields.pets -%}
<label for="petName">Which pet is this for?</label>
<select id="petName" name="properties[Pet]">
<option value="" disabled selected>Please choose a pet</option>
{% for pet in customer.metafields.customer_fields.pets.value -%}
<option value="{{ pet.name }} - {{ pet.breed }} ({{ pet.type }})">
{{ pet.name }}
</option>
{%- endfor %}
</select>
{%- endif %}
You can learn more about accessing metafields and collecting line item properties in Shopify’s developer documentation.
Cart attributes
Let’s suppose you have a business that sells personalized meal kits and customers are able to save any allergies they might have to their account. The allergy list is saved as a list.single_line_text_field
type customer metafield with the following namespace and key:
- namespace:
customer_fields
- key:
allergies
The value of the metafield is an array of strings, like the following:
["Peanuts", "Shellfish", ... ]
If you want to include this allergy list with each order so that the fulfillment team can ensure there are no allergens in the order, then you can add the following code snippet inside the cart form.
{% if customer and customer.metafields.customer_fields.allergies.value != blank -%}
{%- assign allergies = customer.metafields.customer_fields.allergies.value | join: ', ' -%}
<input type="hidden" name="attributes[Allergies]" value="{{ allergies }}">
{%- endif %}
You can learn more about accessing metafields and collecting cart attributes in Shopify’s developer documentation.
Build with Customer Fields
Customer Fields is more than just a form builder. It’s a platform for developers to build unique experiences on Shopify using zero-party and first-party data.
See documentation