Datastore - Upsert

Hello mates!

I have a DataStore and I’m using “upsert”…I would like, on update, sum the new value with the previous one, just like:

value = value + new value or value += newvalue

I think it is a very simple task… :frowning:
Thanks!

In the upsert New Values section of the properties, just add new value to existing:

For example, to increment quantity by one use:

The above is for a single record in a data store. If you have multiple records, you will have to reference the index number of the record you are upserting.

1 Like

You can use the fieldname in backticks as explained in:

2 Likes

Hey Ken! Thanks for your reply!

@George you nailed it!

Many thanks, guys!

What if you want to set/add the value of an input field in quantity field?
This doesn’t work among many other formulas I tried:

`quantity+qty_input.value`

What exactly is not working and what are you trying to achieve - replace the quantity value with the input value, or sum the existing quantity with the input value?

Next to my Add-to-cart-button there’s a number input. I want to add the number of products equal to that input’s value to the cart instead of 1.

Then you don’t need to use the quantity+… You just need to pick the input value as a value for the quantity in data store. And don’t forget to apply the To Number formatter for this value, as the inputs always return strings, but you need a number.

I thought so and tried it that way, but this did the trick:

Thanks @teodor! :clap:

Still having some trouble with this. Using this now to get the quantity of the products in the cart already and add the quantity of the input to it:
(CartStorage.data[0].quantity + (qty_input.value.toNumber()))

CartStorage.data[0].quantity
returns the first cart item’s quantity however, I know that.
So I tried expressions like
CartStorage.data.where(`product_id`, GetProduct.data.ProductData.product_id, '==').quantity
but can’t get the right value.

How could I get the quantity of the item with specific product_id?

Where are you using this? In a repeat region where your products are? Or?

In the cart storage upsert action on ‘add to cart’ button click event, instead of `quantity + 1`
CartStorage is the cart data storage component.
qty_input is the input field to set number of items to add to the cart.

So, if there are X products with specific product_id in the cart already, I want to add qty_input number of products to the cart on the ‘add to cart’ button click event.

Where is your button calling this located? Is it in the repeat region of the items in the cart? Maybe post some screenshots or code of what are you doing and where is this button located.

The button is located on a single product page without a repeat region. There’s a server action on the page that gets the data of that single product which is bound to the page elements:

The circled CartStorage upsert value is where I want to insert the expression to add the qty_input number of items to the cart (in addition to the possibly already in cart present number of products with the same product_id, so if product_id=123 is in the cart 1x already and qty_input is set to 3 and add-to-cart button is clicked, product_id=123 should be added 3x times more, so afterwards product_id=123 is 4x in the cart).

So I want to get the sum of quantity of product_id=123 in the cart already (for which I use the not-working CartStorage.data.where(`product_id`, GetProduct.data.ProductData.product_id, '==').quantity databinding currently) and the quantity input’s value (qty_input.value.toNumber()) and set that in the CartStorage upsert new values quantity field. Hope this is clear.

Got it!

CartStorage.data.where(`product_id`, GetProduct.data.ProductData.Product_id, '==')[0].quantity+qty_input.value.toNumber()

Selecting the first value ([0]) in the where results (which I didn’t realize to be also an array) made it work :+1: