-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate & Retrieve A Variant.py
More file actions
107 lines (94 loc) · 3.47 KB
/
Copy pathCreate & Retrieve A Variant.py
File metadata and controls
107 lines (94 loc) · 3.47 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
"""
Programmer - python_scripts (Abhijith Warrier)
PYTHON SCRIPT TO CREATE & RETRIEVE NEW VARIANT FOR PRODUCT IN Shopify USING GraphQL Admin API
This script demonstrates how to manage product variants in Shopify using the GraphQL Admin API.
It performs the following steps:
1. Retrieve Product Details with Existing Variants – The script first fetches a product’s
details along with its existing variants.
2. Create a New Variant for the Product – It then adds a new variant to the product by
specifying attributes such as SKU, price, and option values.
3. Retrieve the Updated Product – After adding the variant, the script fetches the product
details again to confirm that the new variant has been successfully added.
This ensures that variants are correctly created and associated with the product while adhering
to Shopify’s API structure.
"""
# 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"
# Headers for authentication
HEADERS = {
"X-Shopify-Access-Token": ACCESS_TOKEN,
"Content-Type": "application/json",
}
# Function to retrieve the existing Variants of a Product
def retrieve_product_variants(product_id):
"""
Retrieves all variants of a given product by its ID.
"""
query = """
query getProduct($id: ID!) {
product(id: $id) {
id
title
variants(first: 10) {
edges {
node {
id
title # Variant title
sku
price
}
}
}
}
}
"""
variables = {"id": product_id}
response = requests.post(GRAPHQL_URL, json={"query": query, "variables": variables}, headers=HEADERS)
return response.json()
def create_variant(product_id, sku, price, title):
"""
Creates a new variant for a given product.
"""
mutation = """
mutation createVariant($input: ProductVariantInput!) {
productVariantCreate(input: $input) {
productVariant {
id
title # Variant title
sku
price
}
userErrors {
field
message
}
}
}
"""
variables = {
"input": {
"productId": product_id,
"sku": sku,
"price": price,
"options": [title] # Assign title to variant
}
}
response = requests.post(GRAPHQL_URL, json={"query": mutation, "variables": variables}, headers=HEADERS)
return response.json()
# Define the parameters and values for creating the new Variant
product_id = "gid://shopify/Product/8941400326381"
sku = "PTP-PS-VAR369"
price = "500.00"
variant_title = "1L"
# Retrieve existing variants
print("Existing Variants:", json.dumps(retrieve_product_variants(product_id), indent=2))
# Create new variant
print("Creating New Variant:", json.dumps(create_variant(product_id, sku, price, variant_title), indent=2))
# Retrieve updated variants
print("New Variants:", json.dumps(retrieve_product_variants(product_id), indent=2))