Navigation Menu
steps.md
==================================================================== _ Building an Ecommerce Website with Vanilla JavaScript _
_ Folder structure _
my-vanilla-js-project/ ├── public/ │ ├── images/ │ │ ├── logo.png │ │ └── background.jpg │ └── index.html ├── src/ │ ├── index.html │ ├── main.js │ ├── utils.js │ └── styles.css ├── vite.config.js └── package.json
_ or we can also use _
my-vanilla-js-project/ ├── public/ │ ├── images/ │ │ ├── logo.png │ │ └── background.jpg │ └── index.html ├── index.html ├── main.js ├── styles.css ├── vite.config.js └── package.json
Step 1: Don't Forget To LIKE SHARE & SUBSCRIBE TO THAPA TECHNCIAL YOUTUBE CHANNEL 👉 https://www.youtube.com/thapatechnical
==================================================================== _ Steps to Create a Function for Displaying Product Containers _
- Use
document.querySelector()to select the product container and template container elements from the HTML document.
- Define a function named
showProductContainerthat takes an array of products as input.
- Check if the
productsarray is truthy. If not, returnfalse.
- Iterate over each product in the
productsarray usingforEach().
- Destructure the properties of each product object (brand, category, description, etc.) for easier access.
- Use
document.importNode()to clone the template container for each product.
- Update the cloned template with the product details:
- Set the text content of elements to display product information.
- Set the source attribute of the product image element.
- Calculate and display the actual price based on the product price.
- Add an event listener to a specific element (
.stockElement) within each product template. - Use event delegation to handle click events and pass relevant data (event, product id, stock) to the
homeQuantityToggle()function.
- Append the cloned template with updated product information to the product container element.
- Check if the product container element exists before appending the cloned template.
- You've created a function that dynamically populates the product container with product information based on the provided array of products.
============================================================= _ # Steps to Define the homeQuantityToggle Function _
- Create
quantityToggle.jsto containhomeQuantityToggle.
- Define
homeQuantityToggle(event, id, stock).
- Use
document.querySelector()to select the card element based onid.
- Retrieve product quantity using
querySelector().
- Parse
data-quantityattribute to an integer.
- Increment quantity if event target is
"cartIncrement"and quantity < stock.
- Decrement quantity if event target is
"cartDecrement"and quantity > 1.
- Update text content of product quantity element.
- Set
data-quantityattribute to new quantity.
- Return updated quantity.
You've created homeQuantityToggle in quantityToggle.js, enabling users to update product quantities interactively.
- Select the current card element based on the provided
id. - Retrieve the product quantity from the current card element.
- Retrieve the product price from the current card element.
You've defined the addToCart function, enabling users to add products to their cart with the provided quantity and price.
=================================================================== _ Steps for Retrieving Cart Products from Local Storage _
====================================================================== _ Steps to Update Product Price Before Adding to Local Storage _
- Use
replace()to remove the currency symbol (₹) from the product price.
- Multiply the extracted price by the product quantity to calculate the total price.
- Create an object containing the product
id,quantity, and updatedprice.
- Push the product object to the existing cart array.
- Convert the updated cart array to JSON format using
JSON.stringify(). - Store the JSON string in local storage under a designated key.
If an existing product is found and the local quantity is greater than 1, update the quantity and price of the existing item in the cart.
Ensure that the quantity and price are updated only for the existing item in the cart, not for duplicate entries.
Call the updateCartValue function inside the function responsible for retrieving data from local storage.
Iterate through the filtered cart product data and create DOM elements to display each product in the cart.
Use document.querySelector() to select the container element where the cart products will be displayed.
Implement a function named fetchQuantityFromCartLS to retrieve the actual quantity and price of a product from the cart stored in local storage.
================================================================ _ Handle Increment and Decrement in Add to Cart Page _
Create a function named incrementDecrement to handle the increment and decrement operations for a product in the add to cart page.
Increment the quantity if the increment button is clicked and the current quantity is less than the stock.
Decrement the quantity if the decrement button is clicked and the current quantity is greater than 1.
Implement a function named updateCartProductTotal to update the total cart price value and subtotal.
Initialize a variable initialValue with a value of 0 to use as the initial value for the reduce function.
Use the reduce method to calculate the total order price by summing up the prices of all products in the cart.
Update the text content of the product subtotal and final total elements with the calculated values.
_ Conclusion _
You've implemented functionality to update the total cart price value and subtotal based on the products in the cart. This ensures that users can view the total cost of their order and make informed decisions during checkout.
============================================== _ Extra Information on Vite _
Comments
Post a Comment