-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCreate An Order In Shopify.py
More file actions
146 lines (133 loc) · 4.84 KB
/
Copy pathCreate An Order In Shopify.py
File metadata and controls
146 lines (133 loc) · 4.84 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
"""
Programmer - python_scripts (Abhijith Warrier)
PYTHON SCRIPT TO CREATE AN ORDER IN Shopify USING GraphQL Admin API
This script demonstrates how to create an order in Shopify using the GraphQL Admin API.
It includes customer details, billing and shipping addresses, and line items.
1. Create Order - Uses the orderCreate mutation to create an order with all necessary details.
2. Retrieve Order - Fetches the order details after creation to verify a successful creation.
Orders are fundamental transactions in Shopify, and this script provides an automated way
of handling order creation via API.
"""
# Importing necessary packages
import json
import requests
# Shopify Admin API details
SHOP_URL = "<your_store_name>.myshopify.com"
ACCESS_TOKEN = "<your_store_access_token>"
API_VERSION = "2025-01" # Update as per latest supported version
GRAPHQL_URL = f"https://{SHOP_URL}/admin/api/{API_VERSION}/graphql.json"
# Function to create an Order in Shopify with the provided Customer Data and Line Items Data
def create_order(line_items, customer_details, billing_address, shipping_address):
"""
Creates an order in Shopify.
Returns:
JSON response containing the created order details or error messages
"""
mutation = """
mutation OrderCreate($order: OrderCreateOrderInput!) {
orderCreate(order: $order) {
order {
id
name
email # Email ID of the Customer
customer {
firstName # First name of the Customer
lastName # Last name of the Customer
email # Email ID of the Customer
phone # Contact Number of the Customer
}
billingAddress { # Billing Address Details (dict)
firstName
lastName
address1
address2
city
province
country
zip
phone
}
shippingAddress { # Shipping Address Details (dict)
firstName
lastName
address1
address2
city
province
country
zip
phone
}
lineItems(first: 5) { # List of Product Line Items in the Order
edges {
node {
title
quantity
originalUnitPriceSet {
presentmentMoney {
amount
currencyCode
}
}
}
}
}
totalPriceSet {
presentmentMoney {
amount
currencyCode
}
}
}
userErrors {
field
message
}
}
}
"""
# GraphQL variables containing the Customer Details and Line Items Details
variables = {
"order": {
"email": customer_details["email"],
"billingAddress": billing_address,
"shippingAddress": shipping_address,
"lineItems": line_items,
}
}
# Sending the mutation request to Shopify GraphQL API
response = requests.post(
GRAPHQL_URL,
json={"query": mutation, "variables": variables},
headers={"X-Shopify-Access-Token": ACCESS_TOKEN, "Content-Type": "application/json"},
)
return response.json()
# Details of the Products & Quantity to be associated with the Order
line_items = [
{
"variantId": "gid://shopify/ProductVariant/47696106127597",
"quantity": 1
},
{
"variantId": "gid://shopify/ProductVariant/47696108355821",
"quantity": 2
}
]
# Example of Customer Data for creating an Order in Shopify
customer_details = {
"email": "abhijith@example.com",
}
billing_address = shipping_address = {
"firstName": "Abhijith",
"lastName": "W",
"address1": "123 Main St",
"address2": "Apartment 4B",
"city": "Bangalore",
"province": "Karnataka",
"country": "India",
"zip": "560076",
"phone": "+911234567890"
}
# Create an order and print the response
order_response = create_order(line_items, customer_details, billing_address, shipping_address)
print("Order Creation Response:", json.dumps(order_response, indent=2))