diff --git a/customer.go b/customer.go index 5eb48608..3a2568b4 100644 --- a/customer.go +++ b/customer.go @@ -22,6 +22,7 @@ type CustomerService interface { Update(Customer) (*Customer, error) Delete(int) error ListOrders(int, interface{}) ([]Order, error) + ListTags(interface{}) ([]string, error) // MetafieldsService used for Customer resource to communicate with Metafields resource MetafieldsService @@ -68,6 +69,11 @@ type CustomersResource struct { Customers []Customer `json:"customers"` } +// Represents the result from the customers/tags.json endpoint +type CustomerTagsResource struct { + Tags []string `json:"tags"` +} + // Represents the options available when searching for a customer type CustomerSearchOptions struct { Page int `url:"page,omitempty"` @@ -174,3 +180,11 @@ func (s *CustomerServiceOp) ListOrders(customerID int, options interface{}) ([]O err := s.client.Get(path, resource, options) return resource.Orders, err } + +// ListTags retrieves all unique tags across all customers +func (s *CustomerServiceOp) ListTags(options interface{}) ([]string, error) { + path := fmt.Sprintf("%s/tags.json", customersBasePath) + resource := new(CustomerTagsResource) + err := s.client.Get(path, resource, options) + return resource.Tags, err +} diff --git a/customer_test.go b/customer_test.go index c5f38022..26743ede 100644 --- a/customer_test.go +++ b/customer_test.go @@ -449,3 +449,29 @@ func TestCustomerListOrders(t *testing.T) { order := orders[0] orderTests(t, order) } + +func TestCustomerListTags(t *testing.T) { + setup() + defer teardown() + + httpmock.RegisterResponder( + "GET", + "https://fooshop.myshopify.com/admin/customers/tags.json", + httpmock.NewBytesResponder(200, loadFixture("customer_tags.json")), + ) + + tags, err := client.Customer.ListTags(nil) + if err != nil { + t.Errorf("Customer.ListTags returned error: %v", err) + } + + // Check that tags were parsed + if len(tags) != 2 { + t.Errorf("Customer.ListTags got %v tags, expected: 2", len(tags)) + } + + // Check correct tag was read + if len(tags) > 0 && tags[0] != "tag1" { + t.Errorf("Customer.ListTags got %v as the first tag, expected: 'tag1'", tags[0]) + } +} diff --git a/fixtures/customer_tags.json b/fixtures/customer_tags.json new file mode 100644 index 00000000..7b943340 --- /dev/null +++ b/fixtures/customer_tags.json @@ -0,0 +1,6 @@ +{ + "tags": [ + "tag1", + "another-tag" + ] +} \ No newline at end of file