In the previous article, I explained How to build a cart for your e-commerce store. We were also able to add items to that cart.
Now we’re going to discuss how to delete items from the cart and also, increasing the quantity of a product already in the cart.
Deleting from the cart.
To remove an item or product from the cart is not as difficult as it looks. It’s as simple as slicing an item from the array. Let’s leave the theory and go into the code shall we?
We’re going to use the same array from our previous article How to build a cart for your e-commerce store.
Now there is our cart written as a multi dimensional array (an array within an array). each of the first item in the inner arrays represents the name of the item and the second represents the quantity of that product.
Let’s begin our steps to delete.
We output all the items in our array in html format.
//we'll output these
items in a div called "cartsection"
//cart[i] outputs the item name while cart[i] outputs the quantity.
Now let’s create a rem function to remove or delete the items.
//the index represents it's position on the array. The slice method is used in removing items an array, the first parameter
(Index) tells the browser where to removing from.
And the second tells it
how many items to delete.
//save the new cart in our browser storage.
now with the code above your delete button should be working just fine, now lets go on with increasing and reducing the quantity of products.
before we begin, we’re going to be modifying the display on the cartsection to show both increase button (+) and reduce button (-).
So we add this to the code after the delete button.
//The increase button.
//the reduce button.
Now let’s create the function for it.
Now we’ve completed our cart which includes the basic functions like increase quantity, reduce quantity, remove item and also add item. Feel free to play with the code and include your own CSS code or even bootstrap to improve the user interface and also beautify this cart. We didn’t want to add it here to make our code more readable.
Hope you enjoyed this tutorial, you can leave a feedback on the comment section, let us know what you think and more ways to improve our tutorials, We’ll value your words. Also remember to follow our social media handles to keep updated
You can also reach is via email at