document.addEventListener("DOMContentLoaded", function () {
const addToCartButtons = document.querySelectorAll('[data-action="add-to-cart"]');
addToCartButtons.forEach(button => {
button.addEventListener("click", function (event) {
event.preventDefault(); // Impede o redirecionamento padrão
const form = button.closest("form");
fetch(form.action, {
method: form.method,
body: new FormData(form)
})
.then(response => response.json())
.then(data => {
// Exibe uma notificação de sucesso
const notification = document.createElement("div");
notification.classList.add("added-to-cart-notification");
notification.innerText = "Item adicionado ao carrinho!";
document.body.appendChild(notification);
setTimeout(() => {
notification.classList.add("show");
setTimeout(() => {
notification.classList.remove("show");
notification.remove();
}, 3000); // Oculta após 3 segundos
}, 100);
})
.catch(error => console.error("Erro ao adicionar ao carrinho:", error));
});
});
});