Forum Academy Marketplace Showcase Pricing Features

Manipulating values of a list custom state: possible or not?

Hi everybody, a couple of days have brought to no result and I didn’t find anything in the forum yet.

I’m trying to use a list custom state in my cart page to allow customers to update (+ or -) the number of each item in the cart. I already have done it saving every new value to the DB, but I’m trying to keep the process light with only one save to the DB (button update cart).

My original idea:

  1. on page load a custom list is populated from the cart DB data type (here no problem)
  2. click item + or - increases or decreases the value of the corresponding custom state list n-value. To achieve this I thought I could use :item#current cell’s index.

Well all my efforts brought to nothing:

The problem is the I cannot address a single value of the custom state list, as if it was an array.
Custom state is a list, and to change a single value I have to address a number.

I guess this would be possible with a plugin such as @Keith List Shifter (correct?)
But the real question is: is it also possible out of the box, without plugins?

Thank you to whoever will want to help!
Al the best

1 Like

Hello @gallahad11

: plus item
: minus item

Also … explore using a hidden repeating group to hold your list instead of a custom state (it gives you more options … like conditionality). This is not really needed unless you need more “power” in your logic.

For the concept of adding/substracting items to a list perhaps these videos could shed some more light :smiley:

Hi @cmarchan,
Thank you for your help.
In detail:

Actually I don’t want to add or delete an item from the list, I want to change its value.
My custom state is an ordered list, the new value should be in the same position as the old, not appended last (as :plus item actually does).
So in the example, if I click on plus for item#2, the custom state should pass
from 2,12,1,3
to 2,13,1,3

The videos you posted refer to lists in the DB. The solution is viable, but as I mentioned I’d like to avoid several DB calls.

As I can see there are 2 possible solutions:

  1. change a single value in the custom state list as if it was a normal array (as described above);
  2. consider each RG’s quantity of product as a stand alone number and then, upon click of a “update cart” button:
    2.a) update each corresponding DB field in bulk
    2.b) create a custom state with all the RG quantity values (:plus item as many times as the number of values to append)

Unfortunately solution 2 would imply a for loop in the workflow, and I don’t think this is possible out of the box.

A solution like this, moreover, would be a really cumbersome workaround for something that should be so simple as adding 1 to a value in an array.

Any idea? Am I missing something simple? (after hours on this problem probably I tend to over-complicate things)

You’re not really missing anything, @gallahad11. As you noted in your original post, you could use List Shifter to manage the list of quantity values. I actually demonstrate that in a video I made about shopping carts.

Even then, it’s maybe a touch more complex than it needs to be. (Because List Shifter is a general solution for manipulating lists.)

Your original post got me thinking about this problem again (shopping carts and parallel lists/arrays of values) and I was sort of surprised to see that there’s not any plugins in the store that address this.

So the other day I started a new plugin to help with this use case. The interface part is done but the rest of the code and actions are still in progress.

Of course there are other things you’d want a cart-related plugin to handle, such as using local storage to make the cart persistent, which List Shifter doesn’t help with.

No firm ETA on when I might have this finished, but it’s something I’m working on. In the meantime, to understand how you might address this with List Shifter (and the general issues involved in client-side vs “vanilla Bubble” carts), I’ll post a link to the video I’m talking about…

1 Like

Hi @keith,
Thank you for your answer, detailed as everything I read written by you in this forum.

I read that custom states are not treated as real arrays, but I was hoping that at least some basic manipulations were possible. Unfortunately this is not the case, I really hope that the Bubble team will take care of this asap.

So far I decided to go for the easiest solution, making a call to the db every time a product quantity is modified. Not elegant, but it works and it’s easy.

Thank you again!