Skip to content

Commit c68ac4c

Browse files
authored
Merge pull request #471 from labd/469-request-for-support-for-cart-discount-target
feat(cart_discount): Added support for totalPrice target on cart_disc…
2 parents 748eef5 + ba44c99 commit c68ac4c

File tree

6 files changed

+106
-27
lines changed

6 files changed

+106
-27
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
kind: Fixed
2+
body: Added support for totalPrice target on cart_discount resource
3+
time: 2024-01-19T11:54:29.492858858+01:00

commercetools/resource_cart_discount.go

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -128,33 +128,39 @@ func resourceCartDiscount() *schema.Resource {
128128
Elem: &schema.Resource{
129129
Schema: map[string]*schema.Schema{
130130
"type": {
131-
Description: "Supports lineItems/customLineItems/multiBuyLineItems/multiBuyCustomLineItems/shipping",
131+
Description: "Supports lineItems, customLineItems, multiBuyLineItems, multiBuyCustomLineItems, shipping or totalPrice",
132132
Type: schema.TypeString,
133133
Required: true,
134134
ValidateFunc: validateTargetType,
135135
},
136136
"predicate": {
137-
Description: "LineItems/CustomLineItems/MultiBuyLineItems/MultiBuyCustomLineItems target specific fields",
138-
Type: schema.TypeString,
139-
Optional: true,
137+
Description: "LineItems, CustomLineItems, MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. " +
138+
"If set for another target the value will be ignored",
139+
Type: schema.TypeString,
140+
Optional: true,
140141
},
141142
"trigger_quantity": {
142-
Description: "MultiBuyLineItems/MultiBuyCustomLineItems target specific fields",
143-
Type: schema.TypeInt,
144-
Optional: true,
143+
Description: "MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. " +
144+
"If set for another target the value will be ignored",
145+
Type: schema.TypeInt,
146+
Optional: true,
145147
},
146148
"discounted_quantity": {
147-
Description: "MultiBuyLineItems/MultiBuyCustomLineItems target specific fields",
148-
Type: schema.TypeInt,
149-
Optional: true,
149+
Description: "MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. " +
150+
"If set for another target the value will be ignored",
151+
Type: schema.TypeInt,
152+
Optional: true,
150153
},
151154
"max_occurrence": {
152-
Description: "MultiBuyLineItems/MultiBuyCustomLineItems target specific fields",
153-
Type: schema.TypeInt,
154-
Optional: true,
155+
Description: "MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. " +
156+
"If set for another target the value will be ignored",
157+
Type: schema.TypeInt,
158+
Optional: true,
155159
},
156160
"selection_mode": {
157-
Description: "MultiBuyLineItems/MultiBuyCustomLineItems target specific fields",
161+
Description: "MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. " +
162+
"Can be either Cheapest or MostExpensive. " +
163+
"If set for another target the value will be ignored",
158164
Type: schema.TypeString,
159165
Optional: true,
160166
ValidateFunc: validateSelectionMode,
@@ -193,7 +199,8 @@ func resourceCartDiscount() *schema.Resource {
193199
Default: false,
194200
},
195201
"stacking_mode": {
196-
Description: "Specifies whether the application of this discount causes the following discounts to be ignored",
202+
Description: "Specifies whether the application of this discount causes the following discounts to be ignored. " +
203+
"Can be either Stacking or StopAfterThisDiscount",
197204
Type: schema.TypeString,
198205
Optional: true,
199206
ValidateFunc: validateStackingMode,
@@ -227,9 +234,10 @@ func validateTargetType(val any, key string) (warns []string, errs []error) {
227234
case
228235
"lineItems",
229236
"customLineItems",
237+
"shipping",
238+
"totalPrice",
230239
"multiBuyLineItems",
231-
"multiBuyCustomLineItems",
232-
"shipping":
240+
"multiBuyCustomLineItems":
233241
return
234242
default:
235243
errs = append(errs, fmt.Errorf("%q not a valid value for %q", val, key))
@@ -662,6 +670,10 @@ func flattenCartDiscountTarget(val platform.CartDiscountTarget) []map[string]any
662670
return []map[string]any{{
663671
"type": "shipping",
664672
}}
673+
case platform.CartDiscountTotalPriceTarget:
674+
return []map[string]any{{
675+
"type": "totalPrice",
676+
}}
665677
}
666678

667679
panic("Unable to flatten cart discount target")
@@ -721,6 +733,8 @@ func expandCartDiscountTarget(d *schema.ResourceData) (platform.CartDiscountTarg
721733

722734
case "shipping":
723735
return platform.CartDiscountShippingCostTarget{}, nil
736+
case "totalPrice":
737+
return platform.CartDiscountTotalPriceTarget{}, nil
724738
default:
725739
return nil, fmt.Errorf("target type %s not implemented", input["type"])
726740
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package commercetools
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccCartDiscountTotalPrice(t *testing.T) {
10+
identifier := "totalPrice"
11+
resourceName := "commercetools_cart_discount.totalPrice"
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: testAccCartDiscountTotalPriceConfig(identifier),
20+
Check: resource.ComposeTestCheckFunc(
21+
resource.TestCheckResourceAttr(resourceName, "name.en", "absolute name"),
22+
resource.TestCheckResourceAttr(resourceName, "value.0.type", "absolute"),
23+
resource.TestCheckResourceAttr(resourceName, "value.0.money.0.currency_code", "USD"),
24+
resource.TestCheckResourceAttr(resourceName, "value.0.money.0.cent_amount", "1000"),
25+
resource.TestCheckResourceAttr(resourceName, "value.0.money.1.currency_code", "EUR"),
26+
resource.TestCheckResourceAttr(resourceName, "value.0.money.1.cent_amount", "2000"),
27+
resource.TestCheckResourceAttr(resourceName, "target.0.type", "totalPrice"),
28+
),
29+
},
30+
},
31+
})
32+
}
33+
34+
func testAccCartDiscountTotalPriceConfig(identifier string) string {
35+
return hclTemplate(`
36+
resource "commercetools_cart_discount" "{{ .identifier }}" {
37+
name = {
38+
en = "absolute name"
39+
}
40+
sort_order = "0.9"
41+
predicate = "1=1"
42+
43+
target {
44+
type = "totalPrice"
45+
}
46+
47+
value {
48+
type = "absolute"
49+
money {
50+
currency_code = "USD"
51+
cent_amount = 1000
52+
}
53+
money {
54+
currency_code = "EUR"
55+
cent_amount = 2000
56+
}
57+
}
58+
}
59+
`, map[string]any{
60+
"identifier": identifier,
61+
})
62+
}

commercetools/resource_discount_code.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ func resourceDiscountCode() *schema.Resource {
7676
Optional: true,
7777
},
7878
"max_applications": {
79-
Description: "The discount code can only be applied the specified times overall" +
79+
Description: "The discount code can only be applied the specified times overall. " +
8080
"Note that due to an engine constraint 0 cannot be set for this field, " +
8181
"so possible values are either larger than 0 or not set",
8282
Type: schema.TypeInt,

docs/resources/cart_discount.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ resource "commercetools_cart_discount" "my-cart-discount" {
169169
- `is_active` (Boolean) Only active discount can be applied to the cart
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)
172-
- `stacking_mode` (String) Specifies whether the application of this discount causes the following discounts to be ignored
172+
- `stacking_mode` (String) Specifies whether the application of this discount causes the following discounts to be ignored. Can be either Stacking or StopAfterThisDiscount
173173
- `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))
174174
- `valid_from` (String)
175175
- `valid_until` (String)
@@ -222,12 +222,12 @@ Optional:
222222

223223
Required:
224224

225-
- `type` (String) Supports lineItems/customLineItems/multiBuyLineItems/multiBuyCustomLineItems/shipping
225+
- `type` (String) Supports lineItems, customLineItems, multiBuyLineItems, multiBuyCustomLineItems, shipping or totalPrice
226226

227227
Optional:
228228

229-
- `discounted_quantity` (Number) MultiBuyLineItems/MultiBuyCustomLineItems target specific fields
230-
- `max_occurrence` (Number) MultiBuyLineItems/MultiBuyCustomLineItems target specific fields
231-
- `predicate` (String) LineItems/CustomLineItems/MultiBuyLineItems/MultiBuyCustomLineItems target specific fields
232-
- `selection_mode` (String) MultiBuyLineItems/MultiBuyCustomLineItems target specific fields
233-
- `trigger_quantity` (Number) MultiBuyLineItems/MultiBuyCustomLineItems target specific fields
229+
- `discounted_quantity` (Number) MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. If set for another target the value will be ignored
230+
- `max_occurrence` (Number) MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. If set for another target the value will be ignored
231+
- `predicate` (String) LineItems, CustomLineItems, MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. If set for another target the value will be ignored
232+
- `selection_mode` (String) MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. Can be either Cheapest or MostExpensive. If set for another target the value will be ignored
233+
- `trigger_quantity` (Number) MultiBuyLineItems or MultiBuyCustomLineItems target specific fields. If set for another target the value will be ignored

docs/resources/discount_code.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ resource "commercetools_discount_code" "my_discount_code" {
5454
- `description` (Map of String) [LocalizedString](https://docs.commercetools.com/api/types#localizedstring)
5555
- `groups` (List of String) The groups to which this discount code belong
5656
- `is_active` (Boolean)
57-
- `max_applications` (Number) The discount code can only be applied maxApplications times
58-
- `max_applications_per_customer` (Number) The discount code can only be applied maxApplicationsPerCustomer times per customer
57+
- `max_applications` (Number) The discount code can only be applied the specified times overall. Note that due to an engine constraint 0 cannot be set for this field, so possible values are either larger than 0 or not set
58+
- `max_applications_per_customer` (Number) The discount code can only be applied the specified times per customer. Note that due to an engine constraint 0 cannot be set for this field, so possible values are either larger than 0 or not set
5959
- `name` (Map of String) [LocalizedString](https://docs.commercetools.com/api/types#localizedstring)
6060
- `predicate` (String) [Cart Predicate](https://docs.commercetools.com/api/projects/predicates#cart-predicates)
6161
- `valid_from` (String) The time from which the discount can be applied on a cart. Before that time the code is invalid

0 commit comments

Comments
 (0)