@@ -84,7 +82,7 @@ function fetchCartData(item, index) { // Added index parameter to the function
Forest Green Strap, Regular
Seller: Ezig
-
₹${formatIndianRupee(afterDiscountPrice)} ₹${formatIndianRupee(item.price)} ${discount}% off
+
₹${formatIndianRupee(afterDiscontPrice)} ₹${formatIndianRupee(item.price)} ${discont}% off
1 offer applied
@@ -96,9 +94,21 @@ function fetchCartData(item, index) { // Added index parameter to the function
-
 
-
 
-
+
 
+
 
+
@@ -110,88 +120,69 @@ function fetchCartData(item, index) { // Added index parameter to the function
`;
}
-
-// Function to increase the quantity of a specific item
-function increaseQuantity(index) { // Added function to handle increasing quantity
- const input = document.getElementById(`quantity-${index}`); // Use index to target specific input
- input.value = Number(input.value) + 1;
-}
-
-// Function to decrease the quantity of a specific item
-function decreaseQuantity(index) { // Added function to handle decreasing quantity
- const input = document.getElementById(`quantity-${index}`); // Use index to target specific input
- const currentValue = Number(input.value);
- if (currentValue > 1) {
- input.value = currentValue - 1;
- } else {
- const productName = input.closest('.cartItmListInviewCart').querySelector('.remove').getAttribute('data-name');
- confirmRemoveItem(productName);
- }
-}
-
// Function to confirm and remove an item from the cart
function confirmRemoveItem(name) {
- const confirmRemove = confirm(`Are you sure you want to remove "${name}" from the cart?`);
- if (confirmRemove) {
- removeItem(name);
- }
+ const confirmRemove = confirm(`Are you sure you want to remove "${name}" from the cart?`);
+ if (confirmRemove) {
+ removeItem(name);
+ }
}
// Function to remove an item from the cart
function removeItem(name) {
- let existingData = getFromLocalStorage("filteredProducts") || [];
- const updatedData = existingData.filter(item => item.name !== name);
- localStorage.setItem("filteredProducts", JSON.stringify(updatedData));
+ let existingData = getFromLocalStorage("filteredProducts") || [];
+ const updatedData = existingData.filter(item => item.name !== name);
+ localStorage.setItem("filteredProducts", JSON.stringify(updatedData));
- // Update the displayed cart and price details
- searchFetch(updatedData);
+ // Update the displayed cart and price details
+ searchFetch(updatedData);
- // Show success popup
- showPopup(`"${name}" has been removed from your cart successfully.`);
+ // Show success popup
+ showPopup(`"${name}" has been removed from your cart successfully.`);
}
// Function to show a popup message
function showPopup(message) {
- const popup = document.getElementById('popupCartItem');
- popup.textContent = message;
- popup.classList.remove('hidden');
- popup.classList.add('visible');
-
- // Hide the popup after 1 second
- setTimeout(() => {
- popup.classList.remove('visible');
- popup.classList.add('hidden');
- }, 3000);
+ const popup = document.getElementById('popupCartItem');
+ popup.textContent = message;
+ popup.classList.remove('hidden');
+ popup.classList.add('visible');
+
+ // Hide the popup after 1 second
+ setTimeout(() => {
+ popup.classList.remove('visible');
+ popup.classList.add('hidden');
+ }, 3000);
}
// Function to show or hide cart based on product list
function updateCartDisplay(filteredProducts) {
- let cartInProduct = document.getElementById("cardInProduct");
- let cartisEmpty = document.getElementById("cardisEmpty");
-
- if (filteredProducts.length === 0) {
- cartInProduct.style.display = "none";
- cartisEmpty.style.display = "block";
- } else {
- cartInProduct.style.display = "block";
- cartisEmpty.style.display = "none";
- }
+ let cartInProduct = document.getElementById("cardInProduct");
+ let cartisEmpty = document.getElementById("cardisEmpty");
+
+ if (filteredProducts.length === 0) {
+ cartInProduct.style.display = "none";
+ cartisEmpty.style.display = "block";
+ } else {
+ cartInProduct.style.display = "block";
+ cartisEmpty.style.display = "none";
+ }
}
// Function to update the price detail section
function updatePriceDetail(products) {
- function formatIndianRupee(number) {
- const parts = number.toString().split(".");
- const integerPart = parts[0].replace(/\B(?=(\d{3})+(?!d))/g, ",");
- const formattedNumber = parts.length > 1 ? integerPart + "." + parts[1] : integerPart;
- return formattedNumber;
- }
- totalItems = products.length;
- totalMRP = products.reduce((acc, item) => acc + (item.price), 0);
- totalPrice = products.reduce((acc, item) => acc + (Math.round((100 - ((Math.floor(item.rating * (parseInt((item.price.toString()).slice(0, 2))) / 10)))) * item.price / 100)), 0);
- totalDiscount = totalMRP - totalPrice
-
- let priceDetail = `
+ function formatIndianRupee(number) {
+ const parts = number.toString().split(".");
+ const integerPart = parts[0].replace(/\B(?=(\d{3})+(?!\d))/g, ",");
+ const formattedNumber = parts.length > 1 ? integerPart + "." + parts[1] : integerPart;
+ return formattedNumber;
+ }
+ totalItems = products.length;
+ totalMRP = products.reduce((acc, item) => acc + (item.price), 0);
+ totalPrice = products.reduce((acc, item) => acc + (Math.round((100 - ((Math.floor(item.rating * (parseInt((item.price.toString()).slice(0, 2))) / 10)))) * item.price / 100)), 0);
+ totalDiscount = totalMRP - totalPrice
+
+ let priceDetail = `
Price (${totalItems} items)
@@ -206,28 +197,32 @@ function updatePriceDetail(products) {
Free
-
Total Amount
-
₹${formatIndianRupee(totalPrice)}
+
Total Amount
+
₹${formatIndianRupee(totalPrice)}
-
`
+
You will save ₹${formatIndianRupee(totalDiscount)} on this order
+
+ `;
- document.getElementById("priceDetail").innerHTML = priceDetail;
+ document.getElementById("priceDetail").innerHTML = priceDetail;
}
-// Fetch and display data
+// Fetch data from the JSON file and filter products based on the query
fetch("https://raw.githubusercontent.com/csathnere/APIs/main/json-ec/product.json")
- .then(response => response.json())
- .then(data => {
- const query = getQueryParameter("query");
- const filteredProducts = data.filter(product => product.name === query);
+ .then(response => response.json())
+ .then(data => {
+ const query = getQueryParameter("query");
+ const filteredProducts = data.filter(product => product.name === query);
+
+ // Save filtered products to local storage without overwriting existing data
+ saveToLocalStorage("filteredProducts", filteredProducts);
- // Save filtered products to local storage without overwriting existing data
- saveToLocalStorage("filteredProducts", filteredProducts);
+ // Retrieve updated filtered products from local storage
+ const savedFilteredProducts = getFromLocalStorage("filteredProducts");
- // Retrieve updated filtered products from local storage
- const savedFilteredProducts = getFromLocalStorage("filteredProducts");
+ // Display the filtered products
+ searchFetch(savedFilteredProducts);
+ })
+ .catch(error => console.error("Error fetching data:", error));
- // Display the filtered products
- searchFetch(savedFilteredProducts);
- })
- .catch(error => console.error("Error fetching data:", error));
\ No newline at end of file
+// end