Sync
Customers
If you want Shopify and Statamic to sync users and customers you need to change some of the default config values.
A note on data access
Customer, address and order data is read through the Shopify Admin API, which is not scoped to a single customer — the token can see the whole store. The customer tags default to the logged in user, but any code that fetches a customer or renders an order by an ID taken from the request (a route parameter, query string, form field) must confirm that ID belongs to the logged in user first. Compare order.customer.id to the current user's shopify_id before displaying it. This fails silently — the wrong order renders without an error.
Link Statamic customers from Shopify
To create a link between Shopify customers and Statamic users you need to ensure your Private Shopify app has customer read and customer write permissions.
You then need to set up the following webhooks:
Customer Create
Add a webhook on Customer Creation that sends the data to Statamic and queues the import of that one customer.
Your URL should point to the following endpoint:
https://YOURSITE/!/shopify/webhook/customer/create
Customer Update
Add a webhook on Customer Update that sends any updated data to Statamic and queues a refresh of that customer.
Your URL should point to the following endpoint:
https://YOURSITE/!/shopify/webhook/customer/update
Customer Delete
This webhook will remove the connection between a user in Statamic and the now deleted customer in Shopify. It will NOT delete the user in Statamic.
Your URL should point to the following endpoint:
https://YOURSITE/!/shopify/webhook/customer/delete
Multi-Store
In multi-store mode, a customer may exist across multiple stores with different Shopify customer IDs. Instead of a single shopify_id field, user records store a map keyed by store handle:
# Statamic user data
shopify_ids:
uk: 706405506930370084
us: 819222771040481239
The customer create/update webhooks write to shopify_ids.{storeHandle} automatically based on the incoming X-Shopify-Shop-Domain header. In single-store mode the existing shopify_id field is used unchanged.
When using the customer tags in multi-store mode, pass the store param to read the correct Shopify ID and use the correct store's API:
{{ shopify:customer store="uk" }} ... {{ /shopify:customer }}
{{ shopify:customer:orders store="us" }} ... {{ /shopify:customer:orders }}
{{ shopify:customer:addresses store="uk" }} ... {{ /shopify:customer:addresses }}
The address form needs the store too, so the address is saved to the customer in that store:
{{ shopify:address_form store="uk" }} ... {{ /shopify:address_form }}
When Statamic updates users in Shopify (see below), a user is updated in each store they're linked to. Someone who isn't linked to any store yet is created in the store set by SHOPIFY_MULTI_STORE_CUSTOMER_STORE (multi_store.customer_store), or the primary store if that isn't set, and is linked to other stores as they shop there and the customer webhooks arrive. If neither is set, new users aren't created in Shopify.
Markets mode is a single Shopify store, so it uses shopify_id and needs no store param for customers.
Allowing Shopify to create users in Statamic
If you want new user creation in Shopify to create a new user in Statamic, then in your shopify.php config file, you need to set create_users_from_shopify to true.
Allowing Statamic to create and update users in Shopify
If you want user creation and changes in Statamic to modify Shopify users, then in your shopify.php config file, you need to set update_users_in_shopify to true.
If you want to modify more than just first_name, last_name, and email you can also optionally specify a different job for update_shopify_user_job.