Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into 529-add-support-for-p…
Browse files Browse the repository at this point in the history
…roject-productsearchindexingmode

# Conflicts:
#	go.mod
  • Loading branch information
demeyerthom committed Nov 1, 2024
2 parents a0c7550 + 0133db9 commit fbc3fa5
Show file tree
Hide file tree
Showing 6 changed files with 143 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .changes/unreleased/Added-20241022-162737.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kind: Added
body: Added stores to cart discounts
time: 2024-10-22T16:27:37.948019802+02:00
37 changes: 37 additions & 0 deletions commercetools/resource_cart_discount.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,16 @@ func resourceCartDiscount() *schema.Resource {
},
},
},
"stores": {
Description: "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",
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},
"sort_order": {
Description: "The string must contain a number between 0 and 1. All matching cart discounts are " +
"applied to a cart in the order defined by this field. A discount with greater sort order is " +
Expand Down Expand Up @@ -301,6 +311,7 @@ func resourceCartDiscountCreate(ctx context.Context, d *schema.ResourceData, m a
SortOrder: d.Get("sort_order").(string),
IsActive: boolRef(d.Get("is_active")),
RequiresDiscountCode: ctutils.BoolRef(d.Get("requires_discount_code").(bool)),
Stores: expandStores(d.Get("stores").(*schema.Set)),
Custom: custom,
StackingMode: &stackingMode,
}
Expand Down Expand Up @@ -377,6 +388,7 @@ func resourceCartDiscountRead(ctx context.Context, d *schema.ResourceData, m any
_ = d.Set("requires_discount_code", cartDiscount.RequiresDiscountCode)
_ = d.Set("stacking_mode", cartDiscount.StackingMode)
_ = d.Set("custom", flattenCustomFields(cartDiscount.Custom))
_ = d.Set("stores", flattenStores(cartDiscount.Stores))
return nil
}

Expand Down Expand Up @@ -514,6 +526,13 @@ func resourceCartDiscountUpdate(ctx context.Context, d *schema.ResourceData, m a
}
}

if d.HasChange("stores") {
stores := expandStores(d.Get("stores").(*schema.Set))
input.Actions = append(
input.Actions,
&platform.CartDiscountSetStoresAction{Stores: stores})
}

err := retry.RetryContext(ctx, 1*time.Minute, func() *retry.RetryError {
_, err := client.CartDiscounts().WithId(d.Id()).Post(input).Execute(ctx)
return utils.ProcessRemoteError(err)
Expand Down Expand Up @@ -762,3 +781,21 @@ func expandSelectionMode(selectionMode string) (platform.SelectionMode, error) {
return "", fmt.Errorf("selection mode %s not implemented", selectionMode)
}
}

func flattenStores(storeKeyReferences []platform.StoreKeyReference) []string {
var storeKeys []string
for _, store := range storeKeyReferences {
storeKeys = append(storeKeys, store.Key)
}

return storeKeys
}

func expandStores(storeKeys *schema.Set) []platform.StoreResourceIdentifier {
storeResourceIdentifiers := make([]platform.StoreResourceIdentifier, 0, storeKeys.Len())
for _, key := range storeKeys.List() {
var keyVal = key.(string)
storeResourceIdentifiers = append(storeResourceIdentifiers, platform.StoreResourceIdentifier{Key: &keyVal})
}
return storeResourceIdentifiers
}
100 changes: 100 additions & 0 deletions commercetools/resource_cart_discount_stores_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package commercetools

import (
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccCartDiscountStores(t *testing.T) {
identifier := "stores"
resourceName := "commercetools_cart_discount.stores"

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProviderFactories: testAccProviders,
CheckDestroy: testAccCheckCartDiscountDestroy,
Steps: []resource.TestStep{
{
Config: testAccCartDiscountWithoutStores(identifier),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "stores.#", "0"),
),
},
{
Config: testAccCartDiscountWithStores(identifier),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckTypeSetElemAttr(resourceName, "stores.*", "my-store"),
),
},
{
Config: testAccCartDiscountWithoutStores(identifier),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(resourceName, "stores.#", "0"),
),
},
},
})
}

func testAccCartDiscountWithoutStores(identifier string) string {
return hclTemplate(`
resource "commercetools_cart_discount" "{{ .identifier }}" {
name = {
en = "fixed name"
}
sort_order = "0.9"
predicate = "1=1"
target {
type = "shipping"
}
value {
type = "fixed"
money {
currency_code = "USD"
cent_amount = 1000
}
}
}
`, map[string]any{
"identifier": identifier,
})
}

func testAccCartDiscountWithStores(identifier string) string {
return hclTemplate(`
resource "commercetools_store" "my-store-{{ .identifier }}" {
key = "my-store"
name = {
en-US = "My store"
}
countries = ["NL", "BE"]
languages = ["nl-NL"]
}
resource "commercetools_cart_discount" "{{ .identifier }}" {
name = {
en = "fixed name"
}
stores = [commercetools_store.my-store-{{ .identifier }}.key]
sort_order = "0.9"
predicate = "1=1"
target {
type = "shipping"
}
value {
type = "fixed"
money {
currency_code = "USD"
cent_amount = 1000
}
}
}
`, map[string]any{
"identifier": identifier,
})
}
1 change: 1 addition & 0 deletions docs/resources/cart_discount.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ resource "commercetools_cart_discount" "my-cart-discount" {
- `key` (String) User-specific unique identifier for a cart discount. Must be unique across a project
- `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)
- `stacking_mode` (String) Specifies whether the application of this discount causes the following discounts to be ignored. Can be either Stacking or StopAfterThisDiscount
- `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
- `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))
- `valid_from` (String)
- `valid_until` (String)
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ go 1.22.0

toolchain go1.22.8

//replace github.com/labd/commercetools-go-sdk v1.5.1 => ../commercetools-go-sdk

require (
github.com/elliotchance/orderedmap/v2 v2.4.0
github.com/elliotchance/pie/v2 v2.9.0
Expand Down
1 change: 0 additions & 1 deletion templates/index.md.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ provider "commercetools" {
client_id = "<your client id>"
client_secret = "<your client secret>"
project_key = "<your project key>"
project_key = "<your project key>"
scopes = "<space seperated list of scopes>"
api_url = "<api url>"
token_url = "<token url>"
Expand Down

0 comments on commit fbc3fa5

Please sign in to comment.