-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate & Retrieve Product Metafields.py
More file actions
122 lines (108 loc) · 4.22 KB
/
Copy pathCreate & Retrieve Product Metafields.py
File metadata and controls
122 lines (108 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
"""
Programmer - python_scripts (Abhijith Warrier)
PYTHON SCRIPT TO CREATE & RETRIEVE PRODUCT METAFIELDS IN Shopify USING GraphQL Admin API
This script focuses on creating & retrieving Metafields for a Shopify Product.
1. Create Metafield – Add a new Metafield to a product using metafieldsSet mutation.
Metafields allow you to store custom data beyond Shopify’s built-in attributes.
2. Retrieve Metafields – All metafields associated with the product are retrieved
to verify that the new metafield has been successfully added.
Metafields are useful for adding custom product details such as specifications, or
internal notes, making them a powerful tool for extending Shopify’s functionality.
"""
# Importing the necessary packages
import json
import requests
# Shopify Admin API details
SHOP_URL = "<your_store_name>.myshopify.com"
ACCESS_TOKEN = "<your_access_token>"
API_VERSION = "2024-01" # Update as per latest supported version
GRAPHQL_URL = f"https://{SHOP_URL}/admin/api/{API_VERSION}/graphql.json"
# Function to create a metafield for a product
def create_metafield(product_id, namespace, key, value, value_type):
"""
Creates a metafield for a given product.
"""
mutation = """
mutation createMetafield($metafields: [MetafieldsSetInput!]!) {
metafieldsSet(metafields: $metafields) {
metafields {
id # The unique Shopify product ID (GraphQL GID format)
namespace # The namespace to categorize the metafield
key # The unique key to identify the metafield
value # The actual data to store in the metafield
type # The data type of the metafield
}
userErrors {
field
message
}
}
}
"""
variables = {
"metafields": [
{
"ownerId": product_id, # Associate the metafield with the product
"namespace": namespace, # Define the category/namespace for the metafield
"key": key, # Unique identifier for the metafield
"value": value, # The value stored in the metafield
"type": value_type # The type of value being stored
}
]
}
response = requests.post(
GRAPHQL_URL,
json={"query": mutation, "variables": variables},
headers={"X-Shopify-Access-Token": ACCESS_TOKEN, "Content-Type": "application/json"},
)
return response.json()
# Function to retrieve all metafields of a product
def retrieve_metafields(product_id):
"""
Retrieves all metafields for a given product.
Arguments:
product_id -- The unique Shopify product ID (GraphQL GID format)
Returns:
JSON response containing all metafields of the product
"""
query = """
query getProductMetafields($id: ID!) {
product(id: $id) {
id
title
metafields(first: 10) { # Retrieves the product's metafields
edges {
node {
id
namespace
key
value
type
}
}
}
}
}
"""
variables = {"id": product_id}
response = requests.post(
GRAPHQL_URL,
json={"query": query, "variables": variables},
headers={"X-Shopify-Access-Token": ACCESS_TOKEN, "Content-Type": "application/json"},
)
return response.json()
# Define parameters and values for creating the new Metafield
product_id = "gid://shopify/Product/<your_product_id>"
namespace = "custom"
key = "Origin"
value = "Python API"
value_type = "single_line_text_field"
retrieve_response = retrieve_metafields(product_id)
print("Existing Product Metafields:", json.dumps(retrieve_response, indent=2))
# Create the Metafield
create_response = create_metafield(product_id, namespace,
key, value, value_type)
print("New Metafield Created:", json.dumps(create_response, indent=2))
# Retrieve the Metafield
retrieve_response = retrieve_metafields(product_id)
print("Updated Product Metafields:", json.dumps(retrieve_response, indent=2))