document.addEventListener("DOMContentLoaded", function() {
function addInvoiceButton() {
// Vérifie si on est sur la page du panier
if (!window.location.pathname.includes('/cart')) return;
let attempts = 0;
let maxAttempts = 10;
// Fonction pour vérifier la connexion de l'utilisateur
function checkUserLoggedIn() {
// Exemple : vérifier si un élément spécifique de la page (comme une partie de l'interface utilisateur) est visible après la connexion
return document.querySelector('.sqs-account-name') !== null; // Vous pouvez ajuster ce sélecteur si nécessaire
}
function tryAddingButton() {
let checkoutButton = document.querySelector('[data-test="cart-checkout-button"]');
if (checkoutButton && !document.getElementById("invoice-order-button") && checkUserLoggedIn()) {
let invoiceButton = document.createElement("button");
invoiceButton.id = "invoice-order-button";
invoiceButton.innerText = "Commander avec Facture";
invoiceButton.style.cssText = "background-color: #f4a261; color: white; border: none; padding: 10px; cursor: pointer; margin-top: 10px; width: 100%; font-size: 16px;";
invoiceButton.addEventListener("click", function() {
let cartItems = document.querySelectorAll('[data-test="cart-item"]');
let orderDetails = [];
cartItems.forEach(item => {
let productName = item.querySelector('[data-test="cart-item-title"]').innerText;
let quantity = item.querySelector('[data-test="cart-item-quantity"]').innerText;
orderDetails.push(`${quantity} x ${productName}`);
});
let orderText = encodeURIComponent(orderDetails.join("\n"));
let customerEmail = prompt("Veuillez entrer votre email pour recevoir la facture :");
if (customerEmail) {
let mailtoLink = `mailto:lucas.favre@gmail.com?subject=Nouvelle Commande&body=Commande de ${customerEmail}:%0A${orderText}`;
window.location.href = mailtoLink;
}
});
checkoutButton.insertAdjacentElement("afterend", invoiceButton);
} else if (attempts < maxAttempts) {
attempts++;
setTimeout(tryAddingButton, 500);
}
}
tryAddingButton();
}
addInvoiceButton();
});