Skip to content

Commit 0133db9

Browse files
authored
Merge pull request #532 from labd/500-commercetools_cart_discount-to-support-stores
feat: add store support on cart discounts
2 parents c61b4e5 + ed445a1 commit 0133db9

File tree

7 files changed

+145
-3
lines changed

7 files changed

+145
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Added
2+
body: Added stores to cart discounts
3+
time: 2024-10-22T16:27:37.948019802+02:00

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ vendor/
1919

2020
/.idea
2121
/.env
22+
/go.work*

commercetools/resource_cart_discount.go

+37
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,16 @@ func resourceCartDiscount() *schema.Resource {
168168
},
169169
},
170170
},
171+
"stores": {
172+
Description: "If a value exists, the Cart Discount applies on Carts having a Store matching any " +
173+
"Store defined for this field. If empty, the Cart Discount applies on all Carts, irrespective of " +
174+
"a Store. Use store keys as references",
175+
Type: schema.TypeSet,
176+
Elem: &schema.Schema{
177+
Type: schema.TypeString,
178+
},
179+
Optional: true,
180+
},
171181
"sort_order": {
172182
Description: "The string must contain a number between 0 and 1. All matching cart discounts are " +
173183
"applied to a cart in the order defined by this field. A discount with greater sort order is " +
@@ -301,6 +311,7 @@ func resourceCartDiscountCreate(ctx context.Context, d *schema.ResourceData, m a
301311
SortOrder: d.Get("sort_order").(string),
302312
IsActive: boolRef(d.Get("is_active")),
303313
RequiresDiscountCode: ctutils.BoolRef(d.Get("requires_discount_code").(bool)),
314+
Stores: expandStores(d.Get("stores").(*schema.Set)),
304315
Custom: custom,
305316
StackingMode: &stackingMode,
306317
}
@@ -377,6 +388,7 @@ func resourceCartDiscountRead(ctx context.Context, d *schema.ResourceData, m any
377388
_ = d.Set("requires_discount_code", cartDiscount.RequiresDiscountCode)
378389
_ = d.Set("stacking_mode", cartDiscount.StackingMode)
379390
_ = d.Set("custom", flattenCustomFields(cartDiscount.Custom))
391+
_ = d.Set("stores", flattenStores(cartDiscount.Stores))
380392
return nil
381393
}
382394

@@ -514,6 +526,13 @@ func resourceCartDiscountUpdate(ctx context.Context, d *schema.ResourceData, m a
514526
}
515527
}
516528

529+
if d.HasChange("stores") {
530+
stores := expandStores(d.Get("stores").(*schema.Set))
531+
input.Actions = append(
532+
input.Actions,
533+
&platform.CartDiscountSetStoresAction{Stores: stores})
534+
}
535+
517536
err := retry.RetryContext(ctx, 1*time.Minute, func() *retry.RetryError {
518537
_, err := client.CartDiscounts().WithId(d.Id()).Post(input).Execute(ctx)
519538
return utils.ProcessRemoteError(err)
@@ -762,3 +781,21 @@ func expandSelectionMode(selectionMode string) (platform.SelectionMode, error) {
762781
return "", fmt.Errorf("selection mode %s not implemented", selectionMode)
763782
}
764783
}
784+
785+
func flattenStores(storeKeyReferences []platform.StoreKeyReference) []string {
786+
var storeKeys []string
787+
for _, store := range storeKeyReferences {
788+
storeKeys = append(storeKeys, store.Key)
789+
}
790+
791+
return storeKeys
792+
}
793+
794+
func expandStores(storeKeys *schema.Set) []platform.StoreResourceIdentifier {
795+
storeResourceIdentifiers := make([]platform.StoreResourceIdentifier, 0, storeKeys.Len())
796+
for _, key := range storeKeys.List() {
797+
var keyVal = key.(string)
798+
storeResourceIdentifiers = append(storeResourceIdentifiers, platform.StoreResourceIdentifier{Key: &keyVal})
799+
}
800+
return storeResourceIdentifiers
801+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
package commercetools
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccCartDiscountStores(t *testing.T) {
10+
identifier := "stores"
11+
resourceName := "commercetools_cart_discount.stores"
12+
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() { testAccPreCheck(t) },
15+
ProviderFactories: testAccProviders,
16+
CheckDestroy: testAccCheckCartDiscountDestroy,
17+
Steps: []resource.TestStep{
18+
{
19+
Config: testAccCartDiscountWithoutStores(identifier),
20+
Check: resource.ComposeTestCheckFunc(
21+
resource.TestCheckResourceAttr(resourceName, "stores.#", "0"),
22+
),
23+
},
24+
{
25+
Config: testAccCartDiscountWithStores(identifier),
26+
Check: resource.ComposeTestCheckFunc(
27+
resource.TestCheckTypeSetElemAttr(resourceName, "stores.*", "my-store"),
28+
),
29+
},
30+
{
31+
Config: testAccCartDiscountWithoutStores(identifier),
32+
Check: resource.ComposeTestCheckFunc(
33+
resource.TestCheckResourceAttr(resourceName, "stores.#", "0"),
34+
),
35+
},
36+
},
37+
})
38+
}
39+
40+
func testAccCartDiscountWithoutStores(identifier string) string {
41+
return hclTemplate(`
42+
resource "commercetools_cart_discount" "{{ .identifier }}" {
43+
name = {
44+
en = "fixed name"
45+
}
46+
sort_order = "0.9"
47+
predicate = "1=1"
48+
49+
target {
50+
type = "shipping"
51+
}
52+
53+
value {
54+
type = "fixed"
55+
money {
56+
currency_code = "USD"
57+
cent_amount = 1000
58+
}
59+
}
60+
}
61+
`, map[string]any{
62+
"identifier": identifier,
63+
})
64+
}
65+
66+
func testAccCartDiscountWithStores(identifier string) string {
67+
return hclTemplate(`
68+
resource "commercetools_store" "my-store-{{ .identifier }}" {
69+
key = "my-store"
70+
name = {
71+
en-US = "My store"
72+
}
73+
countries = ["NL", "BE"]
74+
languages = ["nl-NL"]
75+
}
76+
77+
resource "commercetools_cart_discount" "{{ .identifier }}" {
78+
name = {
79+
en = "fixed name"
80+
}
81+
stores = [commercetools_store.my-store-{{ .identifier }}.key]
82+
sort_order = "0.9"
83+
predicate = "1=1"
84+
85+
target {
86+
type = "shipping"
87+
}
88+
89+
value {
90+
type = "fixed"
91+
money {
92+
currency_code = "USD"
93+
cent_amount = 1000
94+
}
95+
}
96+
}
97+
`, map[string]any{
98+
"identifier": identifier,
99+
})
100+
}

docs/resources/cart_discount.md

+1
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ resource "commercetools_cart_discount" "my-cart-discount" {
170170
- `key` (String) User-specific unique identifier for a cart discount. Must be unique across a project
171171
- `requires_discount_code` (Boolean) States whether the discount can only be used in a connection with a [DiscountCode](https://docs.commercetools.com/api/projects/discountCodes#discountcode)
172172
- `stacking_mode` (String) Specifies whether the application of this discount causes the following discounts to be ignored. Can be either Stacking or StopAfterThisDiscount
173+
- `stores` (Set of String) If a value exists, the Cart Discount applies on Carts having a Store matching any Store defined for this field. If empty, the Cart Discount applies on all Carts, irrespective of a Store. Use store keys as references
173174
- `target` (Block List, Max: 1) Empty when the value has type giftLineItem, otherwise a [CartDiscountTarget](https://docs.commercetools.com/api/projects/cartDiscounts#cartdiscounttarget) (see [below for nested schema](#nestedblock--target))
174175
- `valid_from` (String)
175176
- `valid_until` (String)

go.mod

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ require (
1616
github.com/hashicorp/terraform-plugin-go v0.24.0
1717
github.com/hashicorp/terraform-plugin-mux v0.16.0
1818
github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0
19-
github.com/labd/commercetools-go-sdk v1.6.0
19+
github.com/labd/commercetools-go-sdk v1.7.0
2020
github.com/mitchellh/mapstructure v1.5.0
2121
github.com/stretchr/testify v1.9.0
2222
golang.org/x/oauth2 v0.23.0

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -141,8 +141,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
141141
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
142142
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
143143
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
144-
github.com/labd/commercetools-go-sdk v1.6.0 h1:f+hXCSea6WSsANzPliUZes/qbIBBMyhZF1WhBcnLneM=
145-
github.com/labd/commercetools-go-sdk v1.6.0/go.mod h1:3K76EpprufmZhqmcEZ+lPAKeK7tDUzEq81gT9pzrvyQ=
144+
github.com/labd/commercetools-go-sdk v1.7.0 h1:kJv0rAYZ3CqOCuHB/LluZSPoPrnl55f/x7FNwURhXv0=
145+
github.com/labd/commercetools-go-sdk v1.7.0/go.mod h1:B0nR272yhimVmiR6WS2nTctE7dSq6mehE1hre431wtk=
146146
github.com/mattn/go-colorable v0.1.9/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
147147
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
148148
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=

0 commit comments

Comments
 (0)