Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
Hi everyone,
I am new to working with the Shopify API and currently developing a simple app for a client. Part of this app involves updating a custom metafield in the customer profile. However, when I retrieve customer data and print a list of all metafields, the custom metafield (namespace: custom, key: kundennummer) is not listed, even though the customer has filled out these fields.
Here is the relevant part of my code:
try:
# FIND CUSTOMER NUMBER
client_number = None # Initialize client_number
metafields = shopify.Metafield.find(owner_id=customer_id, owner_resource="customer")
if metafields:
for mf in metafields:
if mf.namespace == "custom" and mf.key == "kundennummer":
client_number = mf.value
print(f"Customer number found: {client_number}")
break
else:
print(f"Customer {customer.id} has no metafields.")
client_number = "unknown"
except Exception as e:
# Error handling
print(f"Error retrieving metafields: {e}")
I am using API version 2024-04, and in the metafield creation, I have enabled both write and read access for API access.
What am I missing or doing wrong?
Please note: when I print a list of all metafields, it shows various metafields from, for example, a cookie banner app, but not the custom metafields I have set up.
Any insights or suggestions would be greatly appreciated!
Solved! Go to the solution
This is an accepted solution.
I needed to access the API via an extra endpoint
endpoint = f"https://{SHOP_NAME}.myshopify.com/admin/api/{API_VERSION}/customers/{customer_id}/metafields.json"
params = {
'metafield[owner_resource]': 'customer',
'limit': 250, # Höchstwert pro Seite (maximal 250),
'metafield[namespace]': 'customer_fields',
}
try:
response = requests.get(endpoint, params=params, auth=(API_KEY, PASSWORD))
response.raise_for_status() # Überprüft, ob ein HTTP-Fehlerstatus zurückgegeben wurde
return response.json().get('metafields', [])
except requests.exceptions.RequestException as e:
print(f"Fehler beim Abrufen der Metafelder: {e}")
return None
Also I had to increase the limit because only the first 100 metafields were shown, hence I never received the correct metafield data before.
Found the solution by myself 🙂
Out of interest, what was the solution? Did you need more scopes?
Liam | Developer Advocate @ Shopify
- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
- To learn more visit Shopify.dev or the Shopify Web Design and Development Blog
This is an accepted solution.
I needed to access the API via an extra endpoint
endpoint = f"https://{SHOP_NAME}.myshopify.com/admin/api/{API_VERSION}/customers/{customer_id}/metafields.json"
params = {
'metafield[owner_resource]': 'customer',
'limit': 250, # Höchstwert pro Seite (maximal 250),
'metafield[namespace]': 'customer_fields',
}
try:
response = requests.get(endpoint, params=params, auth=(API_KEY, PASSWORD))
response.raise_for_status() # Überprüft, ob ein HTTP-Fehlerstatus zurückgegeben wurde
return response.json().get('metafields', [])
except requests.exceptions.RequestException as e:
print(f"Fehler beim Abrufen der Metafelder: {e}")
return None
Also I had to increase the limit because only the first 100 metafields were shown, hence I never received the correct metafield data before.