This commit is contained in:
Oliver
2025-10-09 08:37:53 -03:00
parent 34a2ba3b05
commit 8044cd5c2d
2 changed files with 181 additions and 170 deletions

View File

@@ -1,26 +1,21 @@
// --- Category Configuration --- const TRYNOW_WEBHOOK_URL = "https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c25169c6-4234-4b47-8e74-612b9539da0a";
const CATEGORY_CONFIG = {
3: { showLocation: false, webhook: "" }, // services
4: { showLocation: true, webhook: "https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c76e6b4e-af2f-4bc3-9875-6460d0ffc8e3" }, // hosting
5: { showLocation: false, webhook: "https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/fd20e194-b821-4b1f-814f-7bb93ae16046" }, // Workshops
7: { showLocation: false, webhook: "" }, // modules
};
// --- Helper: Get cookie by name --- // --- Helper: Get cookie value ---
function getCookie(name) { function getCookie(name) {
const match = document.cookie.match(new RegExp('(^| )' + name + '=([^;]+)')); const value = `; ${document.cookie}`;
return match ? decodeURIComponent(match[2]) : null; const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
return null;
} }
// --- Add UTM fields to form using cookies --- // --- Add UTM fields to form ---
function addUtmFields(form) { function addUtmFields(form) {
const utmParams = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"]; const utmParams = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"];
const defaults = { const defaults = {
utm_source: "homepage", utm_source: "homepage",
utm_medium: "direct", utm_medium: "direct",
utm_campaign: "none", utm_campaign: "none"
utm_term: "",
utm_content: ""
}; };
utmParams.forEach(param => { utmParams.forEach(param => {
@@ -31,132 +26,138 @@ function addUtmFields(form) {
input.name = param; input.name = param;
form.appendChild(input); form.appendChild(input);
} }
// Get value from cookie or fallback to default const cookieValue = getCookie(param);
input.value = getCookie(param) || defaults[param]; input.value = cookieValue || defaults[param] || "";
}); });
} }
// --- Create Modal --- // --- Create modal ---
function createModal() { function tryNow_createModal() {
const modal = document.createElement("div"); const modal = document.createElement("div");
modal.id = "buyNowModal"; modal.id = "trynowModal";
Object.assign(modal.style, { modal.style.position = "fixed";
position: "fixed", modal.style.top = "0";
top: "0", modal.style.left = "0";
left: "0", modal.style.width = "100%";
width: "100%", modal.style.height = "100%";
height: "100%", modal.style.backgroundColor = "rgba(0,0,0,0.6)";
backgroundColor: "rgba(0,0,0,0.6)", modal.style.display = "none";
display: "none", modal.style.justifyContent = "center";
justifyContent: "center", modal.style.alignItems = "center";
alignItems: "center", modal.style.zIndex = "1000";
zIndex: "1000"
});
modal.innerHTML = ` modal.innerHTML = `
<div style="background: #fff; padding: 40px 30px; border-radius: 16px; max-width: 500px; width: 90%; position: relative; font-family: 'Arial', sans-serif; box-shadow: 0 10px 25px rgba(0,0,0,0.2);"> <div style="
<span id="closeModal" style="position: absolute; top: 15px; right: 20px; cursor: pointer; font-weight: bold; font-size: 24px; color: #555;">&times;</span> background: #ffffff;
<h2 style="margin-bottom: 15px; font-size: 24px; color: #333;">Order Details</h2> padding: 40px 30px;
<p id="productText" style="margin-bottom: 25px; font-weight: 500; color: #555;"></p> border-radius: 16px;
max-width: 400px;
width: 90%;
position: relative;
font-family: 'Arial', sans-serif;
box-shadow: 0 10px 25px rgba(0,0,0,0.2);
">
<span id="trynowCloseModal" style="
position: absolute;
top: 15px;
right: 20px;
cursor: pointer;
font-weight: bold;
font-size: 24px;
color: #555;
">&times;</span>
<form id="buyForm" style="display: flex; flex-direction: column; gap: 15px;"> <h2 style="margin-bottom: 20px; font-size: 24px; color: #333;">Order Details</h2>
<select name="location" id="locationSelect" required style="display: none; padding: 12px 15px; font-size: 16px; border: 1px solid #ccc; border-radius: 8px; outline: none;">
<form id="trynowForm" style="display: flex; flex-direction: column; gap: 15px;">
<input type="email" name="email" placeholder="Email" required style="padding: 12px 15px; font-size: 16px; border: 1px solid #ccc; border-radius: 8px; outline: none;">
<select name="location" id="trynowLocationSelect" required style="padding: 12px 15px; font-size: 16px; border: 1px solid #ccc; border-radius: 8px; outline: none;">
<option value="">Select Location</option> <option value="">Select Location</option>
<option value="Boston">Boston</option> <option value="Boston">Boston</option>
<option value="Manchester">Manchester</option> <option value="Manchester">Manchester</option>
</select> </select>
<input type="text" name="name" placeholder="Name" required style="padding: 12px 15px; border: 1px solid #ccc; border-radius: 8px;"> <select name="product" required style="padding: 12px 15px; font-size: 16px; border: 1px solid #ccc; border-radius: 8px; outline: none;">
<input type="text" name="company" placeholder="Company" required style="padding: 12px 15px; border: 1px solid #ccc; border-radius: 8px;"> <option value="odoo_19" selected>ODOO</option>
<input type="text" name="country" placeholder="Country" required style="padding: 12px 15px; border: 1px solid #ccc; border-radius: 8px;"> <option value="N8N">n8n</option>
<input type="text" name="street" placeholder="Street" required style="padding: 12px 15px; border: 1px solid #ccc; border-radius: 8px;"> </select>
<div style="display: flex; gap: 10px;">
<input type="text" name="zip" placeholder="ZIP" required style="max-width: 100px; flex: 1 1 0; padding: 12px 10px; border: 1px solid #ccc; border-radius: 8px;">
<input type="text" name="town" placeholder="Town" required style="flex: 2 1 0; padding: 12px 10px; border: 1px solid #ccc; border-radius: 8px;">
</div>
<input type="email" name="email" placeholder="Email" required style="padding: 12px 15px; border: 1px solid #ccc; border-radius: 8px;">
<input type="hidden" name="product">
<input type="hidden" name="price">
<input type="hidden" name="id">
<input type="hidden" name="category">
<button id="submitBuy" type="submit" style="padding: 14px; background: #007BFF; color: #fff; border: none; border-radius: 8px; cursor: pointer; font-size: 18px; font-weight: bold;">Send Order</button> <button type="submit" style="
padding: 14px;
background: #007BFF;
color: #fff;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 18px;
font-weight: bold;
transition: background 0.3s;
">Start free Trial</button>
</form> </form>
<div id="confirmation" style="display: none; margin-top: 20px; padding: 20px; background-color: #e6ffed; color: #056608; border-radius: 12px; text-align: center; font-weight: 500;"> <div id="trynowConfirmation" style="
<p>Thank you for your purchase! 🎉</p> display: none;
<button id="closeConfirmation" style="margin-top: 10px; padding: 10px 20px; background: #007BFF; color: #fff; border: none; border-radius: 8px; cursor: pointer;">Close</button> margin-top: 20px;
padding: 20px;
background-color: #e6ffed;
color: #056608;
border-radius: 12px;
text-align: center;
font-weight: 500;
">
<p>Thank you for your submission! 🎉</p>
<button id="trynowCloseConfirmation" style="
margin-top: 10px;
padding: 10px 20px;
background: #007BFF;
color: #fff;
border: none;
border-radius: 8px;
cursor: pointer;
font-size: 16px;
transition: background 0.3s;
">Close</button>
</div> </div>
</div> </div>
`; `;
document.body.appendChild(modal); document.body.appendChild(modal);
document.getElementById("closeModal").onclick = () => modal.style.display = "none";
document.getElementById("closeConfirmation").onclick = () => { // Add UTM fields after form exists
document.getElementById("confirmation").style.display = "none"; const form = modal.querySelector("#trynowForm");
addUtmFields(form);
// Close modal handlers
document.getElementById("trynowCloseModal").onclick = () => { modal.style.display = "none"; };
document.getElementById("trynowCloseConfirmation").onclick = () => {
document.getElementById("trynowConfirmation").style.display = "none";
modal.style.display = "none"; modal.style.display = "none";
}; };
modal.onclick = e => { if (e.target === modal) modal.style.display = "none"; }; modal.onclick = (e) => { if (e.target === modal) modal.style.display = "none"; };
return modal; return modal;
} }
// --- Open Modal --- // --- Handle form submission ---
function openModal(productHref) { function tryNow_handleFormSubmit() {
const modal = document.getElementById("buyNowModal"); const form = document.getElementById("trynowForm");
const form = modal.querySelector("#buyForm"); const confirmation = document.getElementById("trynowConfirmation");
const confirmation = modal.querySelector("#confirmation");
modal.style.display = "flex";
form.style.display = "flex";
confirmation.style.display = "none";
const parts = productHref.split("/").filter(Boolean);
const [id, price, category, ...productParts] = parts;
const product = decodeURIComponent(productParts.join("/"));
const categoryNum = parseInt(category, 10);
const config = CATEGORY_CONFIG[categoryNum] || { showLocation: false, webhook: "" };
const locationSelect = document.getElementById("locationSelect");
if (config.showLocation) {
locationSelect.style.display = "block";
locationSelect.setAttribute("required", "true");
} else {
locationSelect.style.display = "none";
locationSelect.removeAttribute("required");
locationSelect.value = "";
}
form.querySelector('input[name="id"]').value = id || "";
form.querySelector('input[name="price"]').value = price || "0";
form.querySelector('input[name="category"]').value = category || "";
form.querySelector('input[name="product"]').value = product || "Unknown";
form.dataset.webhook = config.webhook || "";
modal.querySelector("#productText").textContent = `You will buy ${product} for $${price}.`;
addUtmFields(form); // Read values from cookies
}
// --- Handle Form Submit ---
function handleFormSubmit() {
const form = document.getElementById("buyForm");
const confirmation = document.getElementById("confirmation");
form.addEventListener("submit", async (e) => { form.addEventListener("submit", async (e) => {
e.preventDefault(); e.preventDefault();
// Update UTM fields from cookies
addUtmFields(form); addUtmFields(form);
const data = Object.fromEntries(new FormData(form).entries()); const data = {};
const webhookUrl = form.dataset.webhook; new FormData(form).forEach((value, key) => (data[key] = value));
if (!webhookUrl) {
alert("No webhook configured for this category.");
return;
}
try { try {
const res = await fetch(webhookUrl, { const res = await fetch(TRYNOW_WEBHOOK_URL, {
method: "POST", method: "POST",
headers: { "Content-Type": "application/json" }, headers: { "Content-Type": "application/json" },
body: JSON.stringify(data), body: JSON.stringify(data)
}); });
if (res.ok) { if (res.ok) {
@@ -173,16 +174,26 @@ function handleFormSubmit() {
}); });
} }
// --- Attach Buttons --- // --- Attach "Try Now" buttons ---
function attachButtons() { function tryNow_attachButtons() {
const buttons = Array.from(document.querySelectorAll("button, a")); const buttons = Array.from(document.querySelectorAll("a, button"));
buttons.forEach(btn => { buttons.forEach(btn => {
const text = btn.textContent.trim(); if (btn.textContent && btn.textContent.trim() === "Try Now") {
if (text === "Buy Now" || text === "Book Now") {
btn.addEventListener("click", (e) => { btn.addEventListener("click", (e) => {
e.preventDefault(); e.preventDefault();
const href = btn.getAttribute("href") || btn.dataset.product || "Unknown/0/0"; const modal = document.getElementById("trynowModal");
openModal(href); if (modal) {
modal.style.display = "flex";
// Reset modal state
const form = document.getElementById("trynowForm");
const confirmation = document.getElementById("trynowConfirmation");
if (form && confirmation) {
form.style.display = "flex";
confirmation.style.display = "none";
addUtmFields(form); // refresh UTM fields each open
}
}
}); });
} }
}); });
@@ -190,8 +201,8 @@ function attachButtons() {
// --- Initialize --- // --- Initialize ---
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", () => {
createModal(); tryNow_createModal();
handleFormSubmit(); tryNow_handleFormSubmit();
attachButtons(); tryNow_attachButtons();
}); });

File diff suppressed because one or more lines are too long