Best Way to Calculate a Discount

I have a registration page where users can sign up for multiple items, with prices changing based on their selections. At the end of the flow, I’ve added a section for applying fixed or percentage-based discounts.

I’m using an “add to cart” function, and the cart is saved in the user’s database to gather the total price of all selected items. Updating the price based on user selections works fine.

The issue arises when a user applies a discount code—I’m unsure how to calculate and update the discounted total, especially if the user goes back and changes their cart items.

Any suggestions on how to handle this?

Total price should be dynamic. You just need to sum all the cart items total prices and multiply with the discount. Whenever a user changes the items in the cart, the result will be shown correctly. Please let me know if everything is clear for you

Yeah, that makes sense, but what is the best way to make this dynamic?

Thanks for responding!

Can I see your database objects please?

Here is what the database looks like for a cart item that’s added to the user’s database which is List of Cart Items.

Can I see your product object?

Are you referring to the product object they are purchasing? Here is the division object that the player is registering for. You can see the Tickets-value for the division is there, and the Tickets-value-freeAgent. Those are created by the organizer. So if someone select free agent, their ticket value will change.

I tried using a custom state that triggers every time a user does something that would update the total cost, and that only works when I do step-by-step. The screenshot below is how I’m calculating the total price.

I found a workaround for my issue:

Each item added creates a cart item, which is then added to the user’s shopping cart. I have a custom state called “Amount” that calculates the total value of the cart when the user reaches the summary group. When a discount is applied, it’s calculated from the Amount state, and a new yes/no state is set to “yes” for “Used Discount.”

If the user leaves the summary group and updates their cart, when they return, a workflow is triggered based on the “Used Discount” state, and the Amount is recalculated. This workaround seems to be working well.

To add on your existing solution:

Ensure you have a discount datatype with field such as ‘minimum amount, discount type(% or fixed), expiry, discount code etc’.

Save the discount user selects with the database.

So if the discounts are on item level, save the selected discount on cart item.

If the discounts are on whole order, save the selected discount on cart order.

When the user selects a discount, save the updated price. So, you need fields ‘Total Base price, Selected Discount, Discounted Amount, Final Price’ Calculate these fields when user selects a discount.

Might get a bit complex, but this approach worked super for me.

1 Like