-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUpdate & Retrieve Product.py
More file actions
104 lines (83 loc) · 3.18 KB
/
Copy pathUpdate & Retrieve Product.py
File metadata and controls
104 lines (83 loc) · 3.18 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
"""
Programmer - python_scripts (Abhijith Warrier)
PYTHON SCRIPT TO UPDATE & RETRIEVE PRODUCTS FROM Shopify USING GraphQL Admin API
This script interacts with the Shopify GraphQL Admin API to retrieve and update product details.
It is designed to perform the following operations:
1. `retrieve_product(product_id)`:
- Fetches details of a specific product using its Shopify Product ID.
- Retrieves key attributes such as title, description, price, and variants.
2. `update_product(product_id, new_title, new_description)`:
- Updates the product's title or description using a GraphQL mutation.
- Ensures that only the provided fields are updated while keeping other details unchanged.
- Retrieves the updated product details to confirm the changes.
The script is structured for reusability and can be extended for bulk product updates.
"""
# Importing the necessary packages
import json
import requests
# Shopify Store Credentials (Replace with your actual store and token)
SHOPIFY_STORE = "your_store.myshopify.com"
ACCESS_TOKEN = "<your_store_access_token>"
API_VERSION = "2024-01"
# Shopify GraphQL API URL
GRAPHQL_URL = f"https://{SHOPIFY_STORE}/admin/api/{API_VERSION}/graphql.json"
# Headers for authentication
HEADERS = {
"Content-Type": "application/json",
"X-Shopify-Access-Token": ACCESS_TOKEN
}
# Function to retrieve product details
def retrieve_product(product_id):
query = """
query getProduct($id: ID!) {
product(id: $id) {
id
title
descriptionHtml
}
}
"""
variables = {"id": product_id}
response = requests.post(GRAPHQL_URL,
json={"query": query, "variables": variables},
headers=HEADERS)
return response.json()
# Function to update product details
def update_product(product_id, new_title, new_description):
mutation = """
mutation updateProduct($id: ID!, $title: String!, $descriptionHtml: String!) {
productUpdate(input: {
id: $id, # Product ID to be updated
title: $title, # New product title
descriptionHtml: $descriptionHtml # New product description
}) {
product {
id # Retrieve updated product ID
title # Retrieve updated product title
descriptionHtml # Retrieve updated product description
}
userErrors {
field # Field that caused an error, if any
message # Error message if update fails
}
}
}
"""
variables = {
"id": product_id,
"title": new_title,
"descriptionHtml": new_description
}
response = requests.post(GRAPHQL_URL,
json={"query": mutation, "variables": variables},
headers=HEADERS)
return response.json()
# Replace with actual product ID
product_id = "gid://shopify/Product/<your_product_id>"
print("Before Update:", json.dumps(retrieve_product(product_id), indent=2))
updated_response = update_product(
product_id,
"Updated Product Title",
"<p>New product description</p>")
print("Update Response:", updated_response)
print("After Update:", json.dumps(retrieve_product(product_id), indent=2))