-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDelete A Product.py
More file actions
90 lines (71 loc) · 2.69 KB
/
Copy pathDelete A Product.py
File metadata and controls
90 lines (71 loc) · 2.69 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
"""
Programmer - python_scripts (Abhijith Warrier)
PYTHON SCRIPT TO DELETE A PRODUCT FROM Shopify AND TRY RETRIEVING IT USING GraphQL Admin API
This script demonstrates how to delete a product from Shopify using the GraphQL Admin API.
It first sends a mutation request to delete a specific product by its ID. After the deletion,
it attempts to retrieve the same product to verify its removal from the store.
If the product is successfully deleted, Shopify will return an error or null response when
trying to fetch it. This process ensures that the product no longer exists in the store’s catalog.
"""
# 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 delete a product
def delete_product(product_id):
mutation = """
mutation productDelete($id: ID!) {
productDelete(input: {id: $id}) {
deletedProductId # The ID of the deleted product
userErrors {
field # The field that caused the error (if any)
message # The error message
}
}
}
"""
# Set up request headers
headers = {
"Content-Type": "application/json",
"X-Shopify-Access-Token": ACCESS_TOKEN,
}
# Request payload with the product ID
payload = {
"query": mutation,
"variables": {"id": product_id}
}
# Send the API Request for deleting the product
response = requests.post(GRAPHQL_URL, json=payload, headers=headers)
return response.json()
# Function to retrieve product details
def retrieve_product(product_id):
query = """
query getProduct($id: ID!) {
product(id: $id) {
id # The ID of the product
title # The title of the product
descriptionHtml # The description of the product
}
}
"""
variables = {"id": product_id}
# API Request to try retrieving the deelted product
response = requests.post(GRAPHQL_URL,
json={"query": query, "variables": variables},
headers=HEADERS)
return response.json()
# Replace with actual product ID
product_id = "gid://shopify/Product/<your_product_id>"
delete_response = delete_product(product_id)
print("Delete Response:", json.dumps(delete_response, indent=2))
print("After Deleting:", json.dumps(retrieve_product(product_id), indent=2))