Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
Hi,
I was wondering if there was a way to query Customers, depending on their custom Metafield values.
I see that this is possible (and incredibly quick and easy I must say) while creating a customer segment. So I can see that internally shopify is able to query customer metafields and use that as a segment definition and show all the customers that fit the criteria, based on the filter that I set in this screen.
Is there a way to access this API? I dont see it anywhere in default admin api documentation for querying customers.
@gunesc for this particular use, you can use the Segments API.
API makes it possible to query the list of members in the segments as well.
@kjchabrathanks, but I've mentioned that already in my question.
And this way is not really efficient where you:
I was more asking if there is a way to do what they are doing with step 1, without all the other steps and get the customer id.
Because in the admin UI, you immediately see members of a segment WITH their customer page links (aka customer id's), as soon as you apply the filter that you created. Which is not the case for the segments api.
@gunesc thanks for the explanation.
Currently with GraphQL Admin API, you can only query Customers based on the properties listed here. Unfortunately metafield by value is not one of them.
You can pull the metafield value by providing key and namespace in the metafield attribute of the customers query. For example:
query {
customers(first: 100) {
edges {
node {
id
metafield(namespace: "custom", key: "favourite_color"){
id
value
}
}
}
pageInfo {
hasNextPage
endCursor
}
}
}
returns an array of customer ids and the metafield values for those customers.
{
"data": {
"customers": {
"edges": [
{
"node": {
"id": "gid://shopify/Customer/7070853234943",
"metafield": null
}
},
{
"node": {
"id": "gid://shopify/Customer/7070853267711",
"metafield": null
}
},
{
"node": {
"id": "gid://shopify/Customer/7070853300479",
"metafield": null
}
},
{
"node": {
"id": "gid://shopify/Customer/7071058657535",
"metafield": {
"id": "gid://shopify/Metafield/26440047132927",
"value": "#00c4f5"
}
}
},
{
"node": {
"id": "gid://shopify/Customer/7071098077439",
"metafield": {
"id": "gid://shopify/Metafield/26157069009151",
"value": "#00d4f0"
}
}
}
],
"pageInfo": {
"hasNextPage": false,
"endCursor": "eyJsYXN0X2lkIjo3MDcxMDk4MDc3NDM5LCJsYXN0X3ZhbHVlIjoiNzA3MTA5ODA3NzQzOSJ9"
}
}
},
"extensions": {
"cost": {
"requestedQueryCost": 20,
"actualQueryCost": 8,
"throttleStatus": {
"maximumAvailable": 2000,
"currentlyAvailable": 1992,
"restoreRate": 100
}
}
}
}
If the metafield value doesn't exist, then the query results display null.
You can use the resulting array and filter out the elements that don't match your criteria or are null. Also the query runs on getting first 100 customers, you will have to have handle pagination via the pageInfo cursor to get all customers. Not sure if this is more helpful.
No it's not 🙂 I'm not going to run a query for all our customers and filter it.
You can pull the metafield value by providing key and namespace in the metafield attribute of the customers query. For example:
And did you just explain to me how to get a metafield value? lol
I am having the same problem. I want to filter customers on base of metafiled value through api.
I can not run a query to first get all customers and then filter it because my store has large no of customers and if i fetch all customers first it will slow down the request