diff --git a/CHANGELOG b/CHANGELOG index 5a7a80ab..6d863d7d 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ - Add FulfillmentEvent resource ([#454](https://github.com/Shopify/shopify_python_api/pull/454)) - Fix for being unable to get the len() of a filter ([#456](https://github.com/Shopify/shopify_python_api/pull/456)) - Add ApplicationCredit resource ([#457](https://github.com/Shopify/shopify_python_api/pull/457)) +- Add support for retrieving all orders using customer_id ([#466](https://github.com/Shopify/shopify_python_api/pull/466)) == Version 8.2.0 - [Feature] Add support for Dynamic API Versioning. When the library is initialized, it will now make a request to diff --git a/shopify/resources/customer.py b/shopify/resources/customer.py index 80be7fc5..e52c8d80 100644 --- a/shopify/resources/customer.py +++ b/shopify/resources/customer.py @@ -1,6 +1,7 @@ from ..base import ShopifyResource from shopify import mixins from .customer_invite import CustomerInvite +from .order import Order class Customer(ShopifyResource, mixins.Metafields): @@ -24,3 +25,6 @@ def search(cls, **kwargs): def send_invite(self, customer_invite=CustomerInvite()): resource = self.post("send_invite", customer_invite.encode()) return CustomerInvite(Customer.format.decode(resource.body)) + + def orders(self): + return Order.find(customer_id=self.id) diff --git a/shopify/resources/order.py b/shopify/resources/order.py index 4e780b0a..2e31a8c3 100644 --- a/shopify/resources/order.py +++ b/shopify/resources/order.py @@ -4,6 +4,15 @@ class Order(ShopifyResource, mixins.Metafields, mixins.Events): + _prefix_source = "/customers/$customer_id/" + + @classmethod + def _prefix(cls, options={}): + customer_id = options.get("customer_id") + if customer_id: + return "%s/customers/%s" % (cls.site, customer_id) + else: + return cls.site def close(self): self._load_attributes_from_response(self.post("close")) diff --git a/test/customer_test.py b/test/customer_test.py index 5819f292..9547b835 100644 --- a/test/customer_test.py +++ b/test/customer_test.py @@ -53,3 +53,12 @@ def test_send_invite_with_params(self): self.assertEqual(customer_invite, json.loads(self.http.request.data.decode("utf-8"))) self.assertIsInstance(customer_invite_response, shopify.CustomerInvite) self.assertEqual(customer_invite['customer_invite']['to'], customer_invite_response.to) + + def test_get_customer_orders(self): + self.fake('customers/207119551', method='GET', body=self.load_fixture('customer')) + customer = shopify.Customer.find(207119551) + self.fake('customers/207119551/orders', method='GET', body=self.load_fixture('orders')) + orders = customer.orders() + self.assertIsInstance(orders[0], shopify.Order) + self.assertEqual(450789469, orders[0].id) + self.assertEqual(207119551, orders[0].customer.id) diff --git a/test/fixtures/orders.json b/test/fixtures/orders.json new file mode 100644 index 00000000..925a4a5d --- /dev/null +++ b/test/fixtures/orders.json @@ -0,0 +1,206 @@ +{ + "orders": [ + { + "id": 450789469, + "email": "bob.norman@hostmail.com", + "closed_at": null, + "created_at": "2008-01-10T11:00:00-05:00", + "updated_at": "2008-01-10T11:00:00-05:00", + "number": 1, + "note": null, + "token": "b1946ac92492d2347c6235b4d2611184", + "gateway": "authorize_net", + "test": false, + "total_price": "598.94", + "subtotal_price": "597.00", + "total_weight": 0, + "total_tax": "11.94", + "taxes_included": false, + "currency": "USD", + "financial_status": "partially_refunded", + "confirmed": true, + "total_discounts": "10.00", + "total_line_items_price": "597.00", + "cart_token": "68778783ad298f1c80c3bafcddeea02f", + "buyer_accepts_marketing": false, + "name": "#1001", + "referring_site": "http://www.otherexample.com", + "landing_site": "http://www.example.com?source=abc", + "cancelled_at": null, + "cancel_reason": null, + "total_price_usd": "598.94", + "checkout_token": "bd5a8aa1ecd019dd3520ff791ee3a24c", + "reference": "fhwdgads", + "user_id": null, + "location_id": null, + "source_identifier": "fhwdgads", + "source_url": null, + "processed_at": "2008-01-10T11:00:00-05:00", + "device_id": null, + "phone": "+557734881234", + "customer_locale": null, + "app_id": null, + "browser_ip": "0.0.0.0", + "client_details": { + "accept_language": null, + "browser_height": null, + "browser_ip": "0.0.0.0", + "browser_width": null, + "session_hash": null, + "user_agent": null + }, + "landing_site_ref": "abc", + "order_number": 1001, + "payment_details": { + "credit_card_number": "•••• •••• •••• 4242", + "credit_card_company": "Visa" + }, + "payment_gateway_names": [ + "bogus" + ], + "tags": "", + "contact_email": "bob.norman@hostmail.com", + "order_status_url": "https://apple.myshopify.com/690933842/orders/b1946ac92492d2347c6235b4d2611184/authenticate?key=ccde591a93123786bd8d257abd970200", + "presentment_currency": "USD", + "total_line_items_price_set": { + "shop_money": { + "amount": "597.00", + "currency_code": "USD" + }, + "presentment_money": { + "amount": "597.00", + "currency_code": "USD" + } + }, + "total_discounts_set": { + "shop_money": { + "amount": "10.00", + "currency_code": "USD" + }, + "presentment_money": { + "amount": "10.00", + "currency_code": "USD" + } + }, + "total_shipping_price_set": { + "shop_money": { + "amount": "0.00", + "currency_code": "USD" + }, + "presentment_money": { + "amount": "0.00", + "currency_code": "USD" + } + }, + "subtotal_price_set": { + "shop_money": { + "amount": "597.00", + "currency_code": "USD" + }, + "presentment_money": { + "amount": "597.00", + "currency_code": "USD" + } + }, + "total_price_set": { + "shop_money": { + "amount": "598.94", + "currency_code": "USD" + }, + "presentment_money": { + "amount": "598.94", + "currency_code": "USD" + } + }, + "total_tax_set": { + "shop_money": { + "amount": "11.94", + "currency_code": "USD" + }, + "presentment_money": { + "amount": "11.94", + "currency_code": "USD" + } + }, + "admin_graphql_api_id": "gid://shopify/Order/450789469", + "billing_address": { + "first_name": "Bob", + "address1": "Chestnut Street 92", + "phone": "555-625-1199", + "city": "Louisville", + "zip": "40202", + "province": "Kentucky", + "country": "United States", + "last_name": "Norman", + "address2": "", + "company": null, + "latitude": 45.41634, + "longitude": -75.6868, + "name": "Bob Norman", + "country_code": "US", + "province_code": "KY" + }, + "shipping_address": { + "first_name": "Bob", + "address1": "Chestnut Street 92", + "phone": "555-625-1199", + "city": "Louisville", + "zip": "40202", + "province": "Kentucky", + "country": "United States", + "last_name": "Norman", + "address2": "", + "company": null, + "latitude": 45.41634, + "longitude": -75.6868, + "name": "Bob Norman", + "country_code": "US", + "province_code": "KY" + }, + "customer": { + "id": 207119551, + "email": "bob.norman@hostmail.com", + "accepts_marketing": false, + "created_at": "2021-02-12T13:51:00-05:00", + "updated_at": "2021-02-12T13:51:00-05:00", + "first_name": "Bob", + "last_name": "Norman", + "orders_count": 1, + "state": "disabled", + "total_spent": "199.65", + "last_order_id": 450789469, + "note": null, + "verified_email": true, + "multipass_identifier": null, + "tax_exempt": false, + "phone": "+16136120707", + "tags": "", + "last_order_name": "#1001", + "currency": "USD", + "accepts_marketing_updated_at": "2005-06-12T11:57:11-04:00", + "marketing_opt_in_level": null, + "tax_exemptions": [], + "admin_graphql_api_id": "gid://shopify/Customer/207119551", + "default_address": { + "id": 207119551, + "customer_id": 207119551, + "first_name": null, + "last_name": null, + "company": null, + "address1": "Chestnut Street 92", + "address2": "", + "city": "Louisville", + "province": "Kentucky", + "country": "United States", + "zip": "40202", + "phone": "555-625-1199", + "name": "", + "province_code": "KY", + "country_code": "US", + "country_name": "United States", + "default": true + } + } + } + ] +} diff --git a/test/order_test.py b/test/order_test.py index 46278a72..a915a1ab 100644 --- a/test/order_test.py +++ b/test/order_test.py @@ -48,3 +48,10 @@ def test_get_order_transaction(self): self.fake('orders/450789469/transactions', method='GET', body=self.load_fixture('transactions')) transactions = order.transactions() self.assertEqual("409.94", transactions[0].amount) + + def test_get_customer_orders(self): + self.fake("customers/207119551/orders", method='GET', body=self.load_fixture('orders'), code=200) + orders = shopify.Order.find(customer_id=207119551) + self.assertIsInstance(orders[0], shopify.Order) + self.assertEqual(450789469, orders[0].id) + self.assertEqual(207119551, orders[0].customer.id)