Creating a Shopping Cart with the Data Store Component

As I explained earlier - use the upsert functionality and update the quantity.
Everything is already explained in the tutorial.

I am following the tutorial and I am using the upsert. Trust me on that :smiley:

Quantity is just not updated at all. And the value is displayed correctly only if I use quantity + 1 (without the backticks).

And also, I have noticed that when trying to edit the upsert I have to add it again before it can be editable. (that’s another topic)

The shopping cart is working very well, but how to record the data from shopping cart to my database?
I created a form with a repeat, using the Data Store as expression, but on Server Connect, the repeat is not imported as an array and I can’t use multi insert query… I’m stuck on it! HELP!

Are you using a hidden form input?

Hey Max! Thanks! Yes!!

<div id="record" is="dmx-repeat" dmx-bind:repeat="cart.data">
		<input id="txtProductId" name="product_id" type="hidden" dmx-bind:value="product_id">
		<input id="txtProductName" name="product_name" type="hidden" dmx-bind:value="product_name">
		<input id="txtPrice" name="price" type="hidden" dmx-bind:value="price">
		<input id="txtQuantity_cart" name="quantity" type="hidden" dmx-bind:value="quantity">
   </div>
1 Like

I’ve just replied to your thread

1 Like

Hello Teodor,
you don’t show the demo of your shopping cart - do you?

Hi and welcome, have a look at

I followed the tutorial and created a checkout page that works just fine. When I to insert the records from my checkout page into the database the first record in the array is duplicated and the rest of the information is lost. I’m sure I have done something silly but I cant seem to figure it out. Anybody got an idea that I could try?

thank you

Maybe this will help:

Not sure, it does not look anything like my code. Here is mine perhaps that will give a clue

I am not sure if I am placing the datastore values in the correct place versus the values to be inserted into the table I call

You have not supplied a value for all of the input fields. The value is obtained from the cart.data repeat and should look similar to dmx-bind:value="quantity"

Thank you Ben. I’ll give it a go today.

Daniel Bell, PhD
CMoN Gourmet Treats
Founder and CEO

So, I am going to bed now but it is my mission to tackle this tutorial again in the morning. One issue I can see popping up is that using local storage it is possible that items that should no longer be available because the date of the show has passed.

So I think this is where I need to compare the cart to the database? How is that done?

Also keep in mind that it’s easy for the visitor to edit the values in their local storage so you should never take those values as accurate. They could change the price, for example.

What I do is pass just the product ID and the quantity to my checkout script. I then do another query using those IDs to get the prices and do the calculation again with the quantities. So even if the local storage says a product is 99p, my checkout will look it up and use the real value of £99 (for example). It would also mean that if a product is no longer available, it won’t be in the query and so the checkout won’t proceed with it.

The other values in the local storage I generally ignore. They are just used to populate the cart every time it’s viewed.

Hope that helps.

Thanks for the input, guys. I am about to embark on this scary journey. My next question is that should I be able to get a working cart using this tutorial will it then be still compatible with the tutorial that @max_gb put out on Stripe Checkout integration?

Yes, that Stripe tutorial was based on this data store tutorial. Check the comments section on how to get the data into your server action.

1 Like

And @max_gb’s Stripe tutorial is excellent. It’s what I used for the cart I built recently :slight_smile:

2 Likes

Do you have a link to yours, @sitestreet? Would love to see it in action.

Yep, it’s in the showcases - ukpaint.co.uk. Select a category, then a product, then choose a colour and size so you can add it to the cart. The cart count updates in the top-right automatically.