I am developing an e-commerce app where users can add products to their shopping cart.
I need to implement an if/else conditional logic on the “Add to Cart” button that does the following:
If the selected product already exists in the logged-in user’s cart, then the action should update the product quantity by incrementing it by 1.
If the product does not exist in the cart, then it should create a new record in the “Cart” collection with an initial quantity of 1 and the related data.
I have tried to find how to do this conditional directly in Adalo, but I can’t find clear options for an if/else block or condition in the button actions.
Could you please let me know if Adalo supports this kind of conditional logic? If yes, could you guide me step-by-step on how to implement it?
My current setup is:
I have collections for Products, Users, and Cart.
The “Add to Cart” button currently has an action to create a new record in “Cart.”
I want to avoid creating duplicate records when adding the same product multiple times; instead, I want the quantity to increase.
I would really appreciate your guidance on how to implement this logic correctly so that the cart screen shows the products with updated quantities.
On that same button create the action of updating the quantity. It should come after the action that creates the record.
After that you will set the first (create) action to run “sometimes” if the logged in users cart contains “zero” of that item. If the user does not have zero the action will not run.
For the second (update) action. Set it to run “sometimes” if the users cart contains more than “zero” of that item.
I hope I’ve explained well enough. I’m away from my computer.
It might be a good idea to have 2 collections: Carts and Cart Items (M:1). When user starts shopping, you create new Cart; and when you add a new Cart Item, you’re connecting it to the current cart.
Your current DB setup might create issues for repeat shoppings
You can access current cart by having a relationship with logged-in user or you can pass it through the screens (the former is more stable)
if you’d like to change the quantity of the existing cart item, you need to have current cart item already available on the screen. You can create it as a single-item list on Product screen.
Cart Item is linked to Products; so you add a button, name it “increase qty”, wrap it in a list of Cart Items, where Logged-in User → Cart → Cart Item → Product → ID is equal to Current Product → ID). This single-item list will appear when Cart Item, in the current Cart, for this Product, already exists. And you can add an action to the button to increase / decrease qty, delete item, etc.
And in order to add a new item, you need to have another button with conditional visibility - condition is similar to above, but you are using Count: Logged-in User → Cart → Cart Items → Count, where Cart Item → Product → ID is equal to Current Product → ID, and whole count is equal to 0.
I’d recommend watching some tutorial videos in Adalo App Academy. This course is pretty good: Create a Robust, Flexible Shopping Cart System. It may give an answer to most of your questions.
Just in case: Adalo Forum is not an official support channel. Forum members (including Community Leaders) help each other by sharing their experience and answering questions.
If you’d like to contact support, submit a ticket here: Right Desk.
I’m also not in front of the builder so my apologies for possible inconsistencies