Skip to main content

Add to cart js

 import { getCartProductFromLS } from "./getCartProducts";

import { showToast } from "./showToast";

import { updateCartValue } from "./updateCartValue";


// -----------------------------------------------------

// to get the cart data from localStorage

// to update the cart value and also to get the data always ready from localStorage

// --------------------------------------------------------

getCartProductFromLS();


// -----------------------------------------------------

// to add the data into localStorage

// --------------------------------------------------------

export const addToCart = (event, id, stock) => {

  let arrLocalStorageProduct = getCartProductFromLS();


  const currentProdElem = document.querySelector(`#card${id}`);

  let quantity = currentProdElem.querySelector(".productQuantity").innerText;

  let price = currentProdElem.querySelector(".productPrice").innerText;

  //   console.log(quantity, price);

  price = price.replace("₹", "");


  let existingProd = arrLocalStorageProduct.find(

    (curProd) => curProd.id === id

  );


  console.log(existingProd);


  if (existingProd && quantity > 1) {

    quantity = Number(existingProd.quantity) + Number(quantity);

    price = Number(price * quantity);

    let updatedCart = { id, quantity, price };


    updatedCart = arrLocalStorageProduct.map((curProd) => {

      return curProd.id === id ? updatedCart : curProd;

    });

    console.log(updatedCart);


    localStorage.setItem("cartProductLS", JSON.stringify(updatedCart));

    //show toast when product added to the cart

    showToast("add", id);

  }


  if (existingProd) {

    // alert("bhai duplicate hai");

    return false;

  }


  //todo Don't Forget To LIKE SHARE & SUBSCRIBE TO THAPA TECHNCIAL YOUTUBE CHANNEL 👉 https://www.youtube.com/thapatechnical


  price = Number(price * quantity);

  quantity = Number(quantity);


  arrLocalStorageProduct.push({ id, quantity, price });

  localStorage.setItem("cartProductLS", JSON.stringify(arrLocalStorageProduct));


  //update the cart button value

  updateCartValue(arrLocalStorageProduct);


  //show toast when product added to the cart

  showToast("add", id);

};

Comments

Popular posts from this blog

Style. Css

 * {   margin: 0;   padding: 0;   box-sizing: border-box; } :root {   font-family: "Urbanist", sans-serif;   line-height: 1.5;   font-weight: 400;   font-synthesis: none;   text-rendering: optimizeLegibility;   -webkit-font-smoothing: antialiased;   -moz-osx-font-smoothing: grayscale;   --buttonColor: #2a2c30;   --secondaryColor: #4b4f56;   --borderColor: #e3eaf0;   --backgroundColor: #f7f7f7;   --backgroundSecondary: #fefefe;   --textColor: #1e1f23;   --anchorColor: #535bf2;   --main-color: #535bf2;   --supporting-color: #ebf3fe;   --glow-color: hsl(186, 91%, 4%); } html {   font-size: 62.5%;   color: var(--textColor); } /* have a look into body once after we done with rest of the styling  */ body {   margin: 0 auto;   padding: 0 32px;   min-width: 320px;   background-color: var(--backgroundColor); } h1, h2, h3, h4, h5, h6 {   line-height: 1.1...

Js

 * { margin: 0; padding: 0; box-sizing: border-box; } :root { font-family: "Urbanist", sans-serif; line-height: 1.5; font-weight: 400; font-synthesis: none; text-rendering: optimizeLegibility; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; --buttonColor: #2a2c30; --secondaryColor: #4b4f56; --borderColor: #e3eaf0; --backgroundColor: #f7f7f7; --backgroundSecondary: #fefefe; --textColor: #1e1f23; --anchorColor: #535bf2; --main-color: #535bf2; --supporting-color: #ebf3fe; --glow-color: hsl(186, 91%, 4%); } html { font-size: 62.5%; color: var(--textColor); } /* have a look into body once after we done with rest of the styling */ body { margin: 0 auto; padding: 0 32px; min-width: 320px; background-color: var(--backgroundColor); } h1, h2, h3, h4, h5, h6 { line-height: 1.1; color: var(--textColor); } h1 { font-size: 3.2rem; } h2 { font-size: 3.2rem; } p, li, a, label { font-size: 1.8rem; ...

Main js

 import "./style.css"; import products from "./api/products.json"; import { showProductContainer } from "./homeProductCards"; // Define a function named `showProductContainer` that takes an array of products as input. showProductContainer(products); //todo Don't Forget To LIKE SHARE & SUBSCRIBE TO THAPA TECHNCIAL YOUTUBE CHANNEL 👉 https://www.youtube.com/thapatechnical