Skip to content

Instantly share code, notes, and snippets.

@viggin543
Created April 19, 2021 09:26
Show Gist options
  • Save viggin543/02bea89f6439cbdbca5ce85e014c9ce4 to your computer and use it in GitHub Desktop.
Save viggin543/02bea89f6439cbdbca5ce85e014c9ce4 to your computer and use it in GitHub Desktop.
func calcTotalDiscount(currentCart *commercetools.Cart, response *cart_service.CartPriceSummeryResponse) {
if len(currentCart.DiscountCodes) != 0 {
for _, lineItem := range currentCart.LineItems {
for _, discountedPrice := range lineItem.DiscountedPricePerQuantity {
response.TotalDiscount += centAmount(discountedPrice.DiscountedPrice.Value) * int32(discountedPrice.Quantity)
}
}
for _,code := range currentCart.DiscountCodes {
if code.State == commercetools.DiscountCodeStateMatchesCart {
for _, cartDiscount := range code.DiscountCode.Obj.CartDiscounts {
switch v := cartDiscount.Obj.Value.(type) {
case commercetools.CartDiscountValueAbsolute:
for _, m := range v.Money {
response.TotalDiscount += centAmount(m)
}
default:
}
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment