From 422649a1d8511f53ebe21a32112c3a28a440cc5d Mon Sep 17 00:00:00 2001 From: yulintan <35462237+yulintan@users.noreply.github.com> Date: Thu, 20 Dec 2018 10:10:40 -0600 Subject: [PATCH] Add order update endpoint (#41) It allows to update notes, customer, address and so on --- order.go | 10 ++++++++++ order_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/order.go b/order.go index 7de0eda8..f14fd997 100644 --- a/order.go +++ b/order.go @@ -18,6 +18,7 @@ type OrderService interface { Count(interface{}) (int, error) Get(int, interface{}) (*Order, error) Create(Order) (*Order, error) + Update(Order) (*Order, error) // MetafieldsService used for Order resource to communicate with Metafields resource MetafieldsService @@ -314,6 +315,15 @@ func (s *OrderServiceOp) Create(order Order) (*Order, error) { return resource.Order, err } +// Update order +func (s *OrderServiceOp) Update(order Order) (*Order, error) { + path := fmt.Sprintf("%s/%d.json", ordersBasePath, order.ID) + wrappedData := OrderResource{Order: &order} + resource := new(OrderResource) + err := s.client.Put(path, wrappedData, resource) + return resource.Order, err +} + // List metafields for an order func (s *OrderServiceOp) ListMetafields(orderID int, options interface{}) ([]Metafield, error) { metafieldService := &MetafieldServiceOp{client: s.client, resource: ordersResourceName, resourceID: orderID} diff --git a/order_test.go b/order_test.go index 9dbb360b..05b62c48 100644 --- a/order_test.go +++ b/order_test.go @@ -223,6 +223,30 @@ func TestOrderCreate(t *testing.T) { } } +func TestOrderUpdate(t *testing.T) { + setup() + defer teardown() + + httpmock.RegisterResponder("PUT", "https://fooshop.myshopify.com/admin/orders/1.json", + httpmock.NewStringResponder(201, `{"order":{"id": 1}}`)) + + order := Order{ + ID: 1, + FinancialStatus: "paid", + FulfillmentStatus: "fulfilled", + } + + o, err := client.Order.Update(order) + if err != nil { + t.Errorf("Order.Update returned error: %v", err) + } + + expected := Order{ID: 1} + if o.ID != expected.ID { + t.Errorf("Order.Update returned id %d, expected %d", o.ID, expected.ID) + } +} + func TestOrderListMetafields(t *testing.T) { setup() defer teardown()