Skip to content

Commit 8977a70

Browse files
authored
Merge pull request #466 from Shopify/paulinakhew-get-orders-using-customer-id
Add support for finding orders using customer_id
2 parents b640f78 + 006e3e6 commit 8977a70

File tree

6 files changed

+236
-0
lines changed

6 files changed

+236
-0
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
- Add FulfillmentEvent resource ([#454](https://github.com/Shopify/shopify_python_api/pull/454))
33
- Fix for being unable to get the len() of a filter ([#456](https://github.com/Shopify/shopify_python_api/pull/456))
44
- Add ApplicationCredit resource ([#457](https://github.com/Shopify/shopify_python_api/pull/457))
5+
- Add support for retrieving all orders using customer_id ([#466](https://github.com/Shopify/shopify_python_api/pull/466))
56

67
== Version 8.2.0
78
- [Feature] Add support for Dynamic API Versioning. When the library is initialized, it will now make a request to

shopify/resources/customer.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from ..base import ShopifyResource
22
from shopify import mixins
33
from .customer_invite import CustomerInvite
4+
from .order import Order
45

56

67
class Customer(ShopifyResource, mixins.Metafields):
@@ -24,3 +25,6 @@ def search(cls, **kwargs):
2425
def send_invite(self, customer_invite=CustomerInvite()):
2526
resource = self.post("send_invite", customer_invite.encode())
2627
return CustomerInvite(Customer.format.decode(resource.body))
28+
29+
def orders(self):
30+
return Order.find(customer_id=self.id)

shopify/resources/order.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,15 @@
44

55

66
class Order(ShopifyResource, mixins.Metafields, mixins.Events):
7+
_prefix_source = "/customers/$customer_id/"
8+
9+
@classmethod
10+
def _prefix(cls, options={}):
11+
customer_id = options.get("customer_id")
12+
if customer_id:
13+
return "%s/customers/%s" % (cls.site, customer_id)
14+
else:
15+
return cls.site
716

817
def close(self):
918
self._load_attributes_from_response(self.post("close"))

test/customer_test.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,12 @@ def test_send_invite_with_params(self):
5353
self.assertEqual(customer_invite, json.loads(self.http.request.data.decode("utf-8")))
5454
self.assertIsInstance(customer_invite_response, shopify.CustomerInvite)
5555
self.assertEqual(customer_invite['customer_invite']['to'], customer_invite_response.to)
56+
57+
def test_get_customer_orders(self):
58+
self.fake('customers/207119551', method='GET', body=self.load_fixture('customer'))
59+
customer = shopify.Customer.find(207119551)
60+
self.fake('customers/207119551/orders', method='GET', body=self.load_fixture('orders'))
61+
orders = customer.orders()
62+
self.assertIsInstance(orders[0], shopify.Order)
63+
self.assertEqual(450789469, orders[0].id)
64+
self.assertEqual(207119551, orders[0].customer.id)

test/fixtures/orders.json

Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
1+
{
2+
"orders": [
3+
{
4+
"id": 450789469,
5+
"email": "[email protected]",
6+
"closed_at": null,
7+
"created_at": "2008-01-10T11:00:00-05:00",
8+
"updated_at": "2008-01-10T11:00:00-05:00",
9+
"number": 1,
10+
"note": null,
11+
"token": "b1946ac92492d2347c6235b4d2611184",
12+
"gateway": "authorize_net",
13+
"test": false,
14+
"total_price": "598.94",
15+
"subtotal_price": "597.00",
16+
"total_weight": 0,
17+
"total_tax": "11.94",
18+
"taxes_included": false,
19+
"currency": "USD",
20+
"financial_status": "partially_refunded",
21+
"confirmed": true,
22+
"total_discounts": "10.00",
23+
"total_line_items_price": "597.00",
24+
"cart_token": "68778783ad298f1c80c3bafcddeea02f",
25+
"buyer_accepts_marketing": false,
26+
"name": "#1001",
27+
"referring_site": "http://www.otherexample.com",
28+
"landing_site": "http://www.example.com?source=abc",
29+
"cancelled_at": null,
30+
"cancel_reason": null,
31+
"total_price_usd": "598.94",
32+
"checkout_token": "bd5a8aa1ecd019dd3520ff791ee3a24c",
33+
"reference": "fhwdgads",
34+
"user_id": null,
35+
"location_id": null,
36+
"source_identifier": "fhwdgads",
37+
"source_url": null,
38+
"processed_at": "2008-01-10T11:00:00-05:00",
39+
"device_id": null,
40+
"phone": "+557734881234",
41+
"customer_locale": null,
42+
"app_id": null,
43+
"browser_ip": "0.0.0.0",
44+
"client_details": {
45+
"accept_language": null,
46+
"browser_height": null,
47+
"browser_ip": "0.0.0.0",
48+
"browser_width": null,
49+
"session_hash": null,
50+
"user_agent": null
51+
},
52+
"landing_site_ref": "abc",
53+
"order_number": 1001,
54+
"payment_details": {
55+
"credit_card_number": "•••• •••• •••• 4242",
56+
"credit_card_company": "Visa"
57+
},
58+
"payment_gateway_names": [
59+
"bogus"
60+
],
61+
"tags": "",
62+
"contact_email": "[email protected]",
63+
"order_status_url": "https://apple.myshopify.com/690933842/orders/b1946ac92492d2347c6235b4d2611184/authenticate?key=ccde591a93123786bd8d257abd970200",
64+
"presentment_currency": "USD",
65+
"total_line_items_price_set": {
66+
"shop_money": {
67+
"amount": "597.00",
68+
"currency_code": "USD"
69+
},
70+
"presentment_money": {
71+
"amount": "597.00",
72+
"currency_code": "USD"
73+
}
74+
},
75+
"total_discounts_set": {
76+
"shop_money": {
77+
"amount": "10.00",
78+
"currency_code": "USD"
79+
},
80+
"presentment_money": {
81+
"amount": "10.00",
82+
"currency_code": "USD"
83+
}
84+
},
85+
"total_shipping_price_set": {
86+
"shop_money": {
87+
"amount": "0.00",
88+
"currency_code": "USD"
89+
},
90+
"presentment_money": {
91+
"amount": "0.00",
92+
"currency_code": "USD"
93+
}
94+
},
95+
"subtotal_price_set": {
96+
"shop_money": {
97+
"amount": "597.00",
98+
"currency_code": "USD"
99+
},
100+
"presentment_money": {
101+
"amount": "597.00",
102+
"currency_code": "USD"
103+
}
104+
},
105+
"total_price_set": {
106+
"shop_money": {
107+
"amount": "598.94",
108+
"currency_code": "USD"
109+
},
110+
"presentment_money": {
111+
"amount": "598.94",
112+
"currency_code": "USD"
113+
}
114+
},
115+
"total_tax_set": {
116+
"shop_money": {
117+
"amount": "11.94",
118+
"currency_code": "USD"
119+
},
120+
"presentment_money": {
121+
"amount": "11.94",
122+
"currency_code": "USD"
123+
}
124+
},
125+
"admin_graphql_api_id": "gid://shopify/Order/450789469",
126+
"billing_address": {
127+
"first_name": "Bob",
128+
"address1": "Chestnut Street 92",
129+
"phone": "555-625-1199",
130+
"city": "Louisville",
131+
"zip": "40202",
132+
"province": "Kentucky",
133+
"country": "United States",
134+
"last_name": "Norman",
135+
"address2": "",
136+
"company": null,
137+
"latitude": 45.41634,
138+
"longitude": -75.6868,
139+
"name": "Bob Norman",
140+
"country_code": "US",
141+
"province_code": "KY"
142+
},
143+
"shipping_address": {
144+
"first_name": "Bob",
145+
"address1": "Chestnut Street 92",
146+
"phone": "555-625-1199",
147+
"city": "Louisville",
148+
"zip": "40202",
149+
"province": "Kentucky",
150+
"country": "United States",
151+
"last_name": "Norman",
152+
"address2": "",
153+
"company": null,
154+
"latitude": 45.41634,
155+
"longitude": -75.6868,
156+
"name": "Bob Norman",
157+
"country_code": "US",
158+
"province_code": "KY"
159+
},
160+
"customer": {
161+
"id": 207119551,
162+
"email": "[email protected]",
163+
"accepts_marketing": false,
164+
"created_at": "2021-02-12T13:51:00-05:00",
165+
"updated_at": "2021-02-12T13:51:00-05:00",
166+
"first_name": "Bob",
167+
"last_name": "Norman",
168+
"orders_count": 1,
169+
"state": "disabled",
170+
"total_spent": "199.65",
171+
"last_order_id": 450789469,
172+
"note": null,
173+
"verified_email": true,
174+
"multipass_identifier": null,
175+
"tax_exempt": false,
176+
"phone": "+16136120707",
177+
"tags": "",
178+
"last_order_name": "#1001",
179+
"currency": "USD",
180+
"accepts_marketing_updated_at": "2005-06-12T11:57:11-04:00",
181+
"marketing_opt_in_level": null,
182+
"tax_exemptions": [],
183+
"admin_graphql_api_id": "gid://shopify/Customer/207119551",
184+
"default_address": {
185+
"id": 207119551,
186+
"customer_id": 207119551,
187+
"first_name": null,
188+
"last_name": null,
189+
"company": null,
190+
"address1": "Chestnut Street 92",
191+
"address2": "",
192+
"city": "Louisville",
193+
"province": "Kentucky",
194+
"country": "United States",
195+
"zip": "40202",
196+
"phone": "555-625-1199",
197+
"name": "",
198+
"province_code": "KY",
199+
"country_code": "US",
200+
"country_name": "United States",
201+
"default": true
202+
}
203+
}
204+
}
205+
]
206+
}

test/order_test.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,10 @@ def test_get_order_transaction(self):
4848
self.fake('orders/450789469/transactions', method='GET', body=self.load_fixture('transactions'))
4949
transactions = order.transactions()
5050
self.assertEqual("409.94", transactions[0].amount)
51+
52+
def test_get_customer_orders(self):
53+
self.fake("customers/207119551/orders", method='GET', body=self.load_fixture('orders'), code=200)
54+
orders = shopify.Order.find(customer_id=207119551)
55+
self.assertIsInstance(orders[0], shopify.Order)
56+
self.assertEqual(450789469, orders[0].id)
57+
self.assertEqual(207119551, orders[0].customer.id)

0 commit comments

Comments
 (0)