For loop on front end for calculation for dynamic prices for booking app

Hello guys,

I’m building booking website with dynamic prices for rooms. Basically idea is that prices could change depending on the dates you book.

For example:
Price between 1.03-31.03 is 100 eur per day.
Price between 1.04-30.04 is 125 eur per day.

If user is booking room from 30.03-02.04 it should calculate the following:
100x2 + 125x2 = 450 eur.

I have two ideas how to solve it:

  1. Make a database record for each date * price. Then search for price days and sum the up. The biggest problem is managing prices. For example if user sets prices for the whole year and than changes I need to modify 365 records.
  2. Make a front end list and than use it in calculation.
    I do not like it because tech savy user can theoretically overwrite my pricing. I would prefect if I could calculate it on back end as well.

Do you have any other ideas? Maybe a plugin?

Hello @glib

One way is to determine first if more than one price applies for a booking.

Let’s say that it does. Through xyz logic you determine that 2 prices apply. Then you can loop through a list of 2 prices to establish each of the two billable line items in your cart.

If only one price applies …. the loop is that for a list of one item.

In an edge case where you have 2+ prices … your loop is that of 2+ items.

Hope this helps :smiley:

Here’s one way to do it:

I used option sets to set the season data, but you could do this with a database thing. Just follow the same format.

Seems to work for me with a few tests. I had to use the “line items” list on the cart data type because of the way Bubble does math. oh well.

I’m sure there’s a more elegant way to manage carts, but this is to demonstrate one way of accomplishing what you want.

The way it works is that we loop through the different “seasons” (pricing periods) and see if the booking date range overlaps with them or is fully contained. In any case, do some date math and then add it all up after the loop.

This topic was automatically closed after 70 days. New replies are no longer allowed.