# How to add numbers in Repeating Group?

Hi,
I am creating a very simple coupon app for restaurants.

Brief Context:
Each user can select multiple coupons. Each coupon has a certain price.
A user can buy different kinds of coupons for different prices.

At the end of the shopping cart, I want to be able to add all the subtotals (15 EUR and 15 EUR in the screenshot below), to calculate the total of the shopping cart. I have tried more than a 100 times but I am not able to figure it out.

I tried using Custom States, but I wasn’t able to calculate the total from the custom states. The custom state I used was for the repeating group that has the shopping cart. The data type of the custom state = number. And finally, I chose the custom state to be a list.

Some problems I encountered in Custom State (Repeating Group, Number Data, List type):

1. If the user chose same number of different coupons- like in the example shown in screenshot, the user chose 5 coupons of 2 different type. This produces a subtotal of 5*3=15 and since he bought two different kinds of coupons, the total would be 15+15=30. The custom state includes 15 only once in the list.That is why I am not able to sum the numbers in RG in repeating group
2. When the user changes the quantities of coupons using the dropdown (in turquoise in screenshot), the custom state list still retains the previous figure in the list in the same coupon cell. Hence, it keeps adding all the figures ever selected by the user (ignoring numerical duplicates).

I am having a hard time figuring out a seemingly simple problem.

Hugs from France!

Honestly, I think the best way to do this is by saving these values in the database. I like to create two data types for this: Shopping Cart and Line Item.

Shopping Cart

• List of Line Items (Line Item, list)
• Customer Name (text)
• Customer Email (text)
• etc…

Line Item

• Coupon (Coupon)
• Quantity (number)
• Total (number)

When a quantity is selected from the dropdown, create or update the line item for this coupon for this customer’s shopping cart: Total = Current Cell’s Coupon’s Price * Current Cell’s Quantity

This will allow you to add or subtract because the Coupon Sub-total is being re-calculated every time. Remember to add the line item to the current user’s Shopping Cart.

The input at the bottom of your screenshot would then be the current user’s Shopping Cart’s List of Line Items’ Total :sum

That’s a high-level look at how I’d approach this. It’s so much easier than messing with states because you have to take into account addition and subtractions and re-calculating every time a change is made.

Hope this helps!

Gaby | Coaching Bubble
Private coaching, courses, and tons of free resources

6 Likes

Thank you so much @romanmg for replying!