r/Web_Development • u/b4andaftr • Aug 19 '19
How can I code add to cart functionality
I wanted to start learning to code a site and so far everything has gone well but I just cant see to find a way to code a thing where I can add something to my cart and remove it, and i've tired looking at youtube but there are very few tutorials that are available and those that are are either too outdated or not helpful.
any help is much appreciated.
1
2
u/alpha7158 Aug 20 '19
Cookies are variables that live in the user's web browser. Therefore one way to manage a cart is to store the product ID and quantity in a cookie, then have the checkout page read this cookie to see what is in the cart.
Sessions build upon cookies to store this information in temporary storage on the web server, storing only an ID in the cookie to allow the session variable to be accessed. So most ecommerce sites that have a basket without you having to log in use this method.
Once you've set a cookie or a session variable, you can simply access and read off an array of values.
You just need to be careful not to store things in the cookies that users can easily manipulate. For example, you wouldn't want to store the price, then use that stored price as part of the checkout process. If you did, someone could easily open browser developer tools to edit the price to whatever they like. This is why you would store the product ID, so you can use that to read the price and identify the purchase against the product.
1
1
u/GuylianWasHier Aug 19 '19
Which technologies do you use?