Skip to content

Instantly share code, notes, and snippets.

@isaacviannadev
Last active April 17, 2023 19:21
Show Gist options
  • Select an option

  • Save isaacviannadev/507147a35bf973acc47c6db57fe2f869 to your computer and use it in GitHub Desktop.

Select an option

Save isaacviannadev/507147a35bf973acc47c6db57fe2f869 to your computer and use it in GitHub Desktop.
function addProduct(id) {
let cart = JSON.parse(localStorage.getItem("products")) || [];
const foundItem = cart.find((product) => product.id === id);
if (foundItem) {
foundItem.quantidade++;
} else {
cart.push({ id: id, qtd: 1 });
}
localStorage.setItem("products", JSON.stringify(cart));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment