diff --git a/public/buyModal.js b/public/buyModal.js
index 6e463fb..ac1c046 100644
--- a/public/buyModal.js
+++ b/public/buyModal.js
@@ -7,16 +7,24 @@ const CATEGORY_CONFIG = {
// Add more categories as needed
};
+
// --- Get URL parameters as an object ---
function getUrlParams() {
return Object.fromEntries(new URLSearchParams(window.location.search).entries());
}
-// --- Add UTM fields to form ---
+// --- Add UTM fields to form with defaults ---
function addUtmFields(form) {
const utmParams = ["utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content"];
const urlParams = getUrlParams();
+ // Define default values
+ const defaults = {
+ utm_source: "homepage",
+ utm_medium: "direct",
+ utm_campaign: "none"
+ };
+
utmParams.forEach(param => {
let input = form.querySelector(`input[name="${param}"]`);
if (!input) {
@@ -25,7 +33,8 @@ function addUtmFields(form) {
input.name = param;
form.appendChild(input);
}
- input.value = urlParams[param] || "";
+ // Use URL param if exists, otherwise fallback to default, else empty string
+ input.value = urlParams[param] || defaults[param] || "";
});
}
diff --git a/public/buyModal.min.js b/public/buyModal.min.js
index 0369d34..913d8d5 100644
--- a/public/buyModal.min.js
+++ b/public/buyModal.min.js
@@ -1 +1 @@
-const CATEGORY_CONFIG={3:{showLocation:!1,webhook:"https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c76e6b4e-af2f-4bc3-9875-6460d0ffc8e3"},4:{showLocation:!0,webhook:"https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c76e6b4e-af2f-4bc3-9875-6460d0ffc8e3"},5:{showLocation:!1,webhook:"https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c76e6b4e-af2f-4bc3-9875-6460d0ffc8e3"},7:{showLocation:!1,webhook:"https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c76e6b4e-af2f-4bc3-9875-6460d0ffc8e3"}};function getUrlParams(){return Object.fromEntries(new URLSearchParams(window.location.search).entries())}function addUtmFields(e){const t=getUrlParams();["utm_source","utm_medium","utm_campaign","utm_term","utm_content"].forEach(o=>{let n=e.querySelector(`input[name="${o}"]`);n||(n=document.createElement("input"),n.type="hidden",n.name=o,e.appendChild(n)),n.value=t[o]||""})}function createModal(){const e=document.createElement("div");return e.id="buyNowModal",Object.assign(e.style,{position:"fixed",top:"0",left:"0",width:"100%",height:"100%",backgroundColor:"rgba(0,0,0,0.6)",display:"none",justifyContent:"center",alignItems:"center",zIndex:"1000"}),e.innerHTML='\n
\n ×\n
Order Details
\n \n\n \n\n
\n
Thank you for your purchase! 🎉
\n \n
\n
\n ',document.body.appendChild(e),document.getElementById("closeModal").onclick=()=>e.style.display="none",document.getElementById("closeConfirmation").onclick=()=>{document.getElementById("confirmation").style.display="none",e.style.display="none"},e.onclick=t=>{t.target===e&&(e.style.display="none")},e}function openModal(e){const t=document.getElementById("buyNowModal"),o=t.querySelector("#buyForm"),n=t.querySelector("#confirmation");t.style.display="flex",o.style.display="flex",n.style.display="none";const d=e.split("/").filter(Boolean),[r,i,a,...c]=d,p=decodeURIComponent(c.join("/")),l=parseInt(a,10),s=CATEGORY_CONFIG[l]||{showLocation:!1,webhook:""},u=document.getElementById("locationSelect");s.showLocation?(u.style.display="block",u.setAttribute("required","true")):(u.style.display="none",u.removeAttribute("required"),u.value=""),o.querySelector('input[name="id"]').value=r||"",o.querySelector('input[name="price"]').value=i||"0",o.querySelector('input[name="category"]').value=a||"",o.querySelector('input[name="product"]').value=p||"Unknown",o.dataset.webhook=s.webhook||"",t.querySelector("#productText").textContent=`You will buy ${p} for $${i}.`,addUtmFields(o)}function handleFormSubmit(){const e=document.getElementById("buyForm"),t=document.getElementById("confirmation");e.addEventListener("submit",async o=>{o.preventDefault(),addUtmFields(e);const n=Object.fromEntries(new FormData(e).entries()),d=e.dataset.webhook;if(d)try{(await fetch(d,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)})).ok?(e.style.display="none",t.style.display="block",e.reset()):alert("Failed to submit form.")}catch(e){console.error(e),alert("Error submitting form.")}else alert("No webhook configured for this category.")})}function attachButtons(){Array.from(document.querySelectorAll("button, a")).forEach(e=>{const t=e.textContent.trim();"Buy Now"!==t&&"Book Now"!==t||e.addEventListener("click",t=>{t.preventDefault();openModal(e.getAttribute("href")||e.dataset.product||"Unknown/0/0")})})}document.addEventListener("DOMContentLoaded",()=>{createModal(),handleFormSubmit(),attachButtons()});
\ No newline at end of file
+const CATEGORY_CONFIG={3:{showLocation:!1,webhook:"https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c76e6b4e-af2f-4bc3-9875-6460d0ffc8e3"},4:{showLocation:!0,webhook:"https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c76e6b4e-af2f-4bc3-9875-6460d0ffc8e3"},5:{showLocation:!1,webhook:"https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c76e6b4e-af2f-4bc3-9875-6460d0ffc8e3"},7:{showLocation:!1,webhook:"https://002-001-5dd6e535-4d1c-46bc-9bd9-42ad4bc5f082.odoo4projects.com/webhook/c76e6b4e-af2f-4bc3-9875-6460d0ffc8e3"}};function getUrlParams(){return Object.fromEntries(new URLSearchParams(window.location.search).entries())}function addUtmFields(e){const t=getUrlParams(),o={utm_source:"homepage",utm_medium:"direct",utm_campaign:"none"};["utm_source","utm_medium","utm_campaign","utm_term","utm_content"].forEach(n=>{let d=e.querySelector(`input[name="${n}"]`);d||(d=document.createElement("input"),d.type="hidden",d.name=n,e.appendChild(d)),d.value=t[n]||o[n]||""})}function createModal(){const e=document.createElement("div");return e.id="buyNowModal",Object.assign(e.style,{position:"fixed",top:"0",left:"0",width:"100%",height:"100%",backgroundColor:"rgba(0,0,0,0.6)",display:"none",justifyContent:"center",alignItems:"center",zIndex:"1000"}),e.innerHTML='\n
\n ×\n
Order Details
\n \n\n \n\n
\n
Thank you for your purchase! 🎉
\n \n
\n
\n ',document.body.appendChild(e),document.getElementById("closeModal").onclick=()=>e.style.display="none",document.getElementById("closeConfirmation").onclick=()=>{document.getElementById("confirmation").style.display="none",e.style.display="none"},e.onclick=t=>{t.target===e&&(e.style.display="none")},e}function openModal(e){const t=document.getElementById("buyNowModal"),o=t.querySelector("#buyForm"),n=t.querySelector("#confirmation");t.style.display="flex",o.style.display="flex",n.style.display="none";const d=e.split("/").filter(Boolean),[r,i,a,...c]=d,p=decodeURIComponent(c.join("/")),l=parseInt(a,10),s=CATEGORY_CONFIG[l]||{showLocation:!1,webhook:""},u=document.getElementById("locationSelect");s.showLocation?(u.style.display="block",u.setAttribute("required","true")):(u.style.display="none",u.removeAttribute("required"),u.value=""),o.querySelector('input[name="id"]').value=r||"",o.querySelector('input[name="price"]').value=i||"0",o.querySelector('input[name="category"]').value=a||"",o.querySelector('input[name="product"]').value=p||"Unknown",o.dataset.webhook=s.webhook||"",t.querySelector("#productText").textContent=`You will buy ${p} for $${i}.`,addUtmFields(o)}function handleFormSubmit(){const e=document.getElementById("buyForm"),t=document.getElementById("confirmation");e.addEventListener("submit",async o=>{o.preventDefault(),addUtmFields(e);const n=Object.fromEntries(new FormData(e).entries()),d=e.dataset.webhook;if(d)try{(await fetch(d,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(n)})).ok?(e.style.display="none",t.style.display="block",e.reset()):alert("Failed to submit form.")}catch(e){console.error(e),alert("Error submitting form.")}else alert("No webhook configured for this category.")})}function attachButtons(){Array.from(document.querySelectorAll("button, a")).forEach(e=>{const t=e.textContent.trim();"Buy Now"!==t&&"Book Now"!==t||e.addEventListener("click",t=>{t.preventDefault();openModal(e.getAttribute("href")||e.dataset.product||"Unknown/0/0")})})}document.addEventListener("DOMContentLoaded",()=>{createModal(),handleFormSubmit(),attachButtons()});
\ No newline at end of file