/**
* Delivery charges transparency (CMA) — PDP + mini basket
*
* PDP (body.ex-product): static notice between price and qty / add-to-cart.
* Mini basket (.minicart_container): delivery line in totals + "add £X more"
* notice, recalculated from $mini_cart on "cart-total-update".
* Cart page (body.ex-cart): progress-to-free-delivery notice under the items table.
*/
(function () {
"use strict";
if (!document.querySelector(".ex-a-2195551")) return;
// ---- Config: confirm figures with the client before go-live ----
const CONFIG = {
freeThreshold: 49, // free delivery at/over this goods value, ex VAT
charge: 5.95, // delivery charge under threshold, ex VAT
vatRate: 0.2, // VAT applied to delivery
currency: "£",
minicartDeliveryRow: true, // add delivery line and include it in VAT + Order total
};
// ---------- helpers ----------
const round2 = (n) => Math.round(n * 100) / 100;
const toNum = (v) => {
const n = parseFloat(String(v == null ? "" : v).replace(/[^0-9.-]/g, ""));
return Number.isFinite(n) ? n : 0;
};
const nf = new Intl.NumberFormat("en-GB", { minimumFractionDigits: 2, maximumFractionDigits: 2 });
const money = (n) => CONFIG.currency + nf.format(n);
const moneyShort = (n) => (Number.isInteger(n) ? CONFIG.currency + n : money(n));
const incVat = (n) => round2(n * (1 + CONFIG.vatRate));
const setText = (el, text) => {
if (el && el.textContent !== text) el.textContent = text;
};
const show = (el, visible) => {
if (!el) return;
const value = visible ? "" : "none";
if (el.style.display !== value) el.style.display = value;
};
const truckIcon =
'';
const buildNotice = (modifier) => {
const el = document.createElement("div");
el.className = "delivery-notice delivery-notice--" + modifier;
el.setAttribute("role", "note");
el.setAttribute("aria-label", "Delivery charges");
el.innerHTML =
'' +
truckIcon +
"" +
'
' +
'' +
'' +
"
";
return el;
};
const fillNotice = (notice, title, sub) => {
setText(notice.querySelector(".delivery-notice__title"), title);
setText(notice.querySelector(".delivery-notice__sub"), sub);
};
// ---------- PDP ----------
const updatePdp = () => {
if (!document.body.classList.contains("ex-product")) return;
document.querySelectorAll(".productaddtocart_container .product-shopping-actions").forEach((actions) => {
if (actions.querySelector(".delivery-notice--pdp")) return;
const threshold = moneyShort(CONFIG.freeThreshold);
const notice = buildNotice("pdp");
fillNotice(
notice,
"Free delivery on orders over " + threshold + " ex VAT",
money(CONFIG.charge) + " ex VAT (" + money(incVat(CONFIG.charge)) + " inc VAT) on orders under " + threshold,
);
const row = document.createElement("div");
row.className = "row pdp-delivery-info-row";
const col = document.createElement("div");
col.className = "col-md-12";
col.appendChild(notice);
row.appendChild(col);
const prices = actions.querySelector(".product-shopping-actions-prices");
const priceRow = prices ? prices.closest(".product-shopping-actions-row") : null;
if (priceRow) {
priceRow.insertAdjacentElement("afterend", row);
} else {
actions.insertAdjacentElement("afterbegin", row);
}
});
};
// ---------- Mini basket ----------
const readCart = () => {
let cart = null;
try {
// works for a global declared with var, let or const
cart = typeof $mini_cart !== "undefined" ? $mini_cart : window.$mini_cart;
} catch (e) {
cart = window.$mini_cart;
}
if (!Array.isArray(cart) || !cart.length || !cart[0]) return null;
const first = cart[0];
const subTotal = toNum(first.cart_sub_total);
const discounted = first.cart_sub_total_with_discount;
const hasDiscount = discounted != null && toNum(discounted) >= 0;
return {
goods: round2(hasDiscount ? toNum(discounted) : subTotal), // ex VAT, after discount
total: round2(toNum(first.cart_total)), // inc VAT, excl. delivery
};
};
const ensureDeliveryRow = (table) => {
let row = table.querySelector("tr.minicart-delivery");
if (row) return row;
row = document.createElement("tr");
row.className = "minicart-delivery";
row.innerHTML =
'Delivery (standard, ex VAT) | ' +
' | ';
const taxRow = table.querySelector("#minicart_tax");
const divider = table.querySelector("tr.devider");
const anchor = taxRow || divider;
if (anchor) {
anchor.parentNode.insertBefore(row, anchor);
} else {
(table.tBodies[0] || table).appendChild(row);
}
return row;
};
const updateMinicart = () => {
document.querySelectorAll(".minicart_container .cart-resume").forEach((resume) => {
const totals = resume.querySelector(".cart-totals");
if (!totals) return;
let notice = totals.querySelector(".delivery-notice--minicart");
if (!notice) {
notice = buildNotice("minicart");
totals.appendChild(notice);
}
const table = totals.querySelector("table");
const existingRow = table ? table.querySelector("tr.minicart-delivery") : null;
const cart = readCart();
if (!cart) {
show(notice, false);
show(existingRow, false);
return;
}
const threshold = moneyShort(CONFIG.freeThreshold);
const remaining = round2(CONFIG.freeThreshold - cart.goods);
const qualifies = remaining <= 0;
const delivery = qualifies ? 0 : CONFIG.charge;
show(notice, true);
notice.classList.toggle("is-qualified", qualifies);
if (qualifies) {
fillNotice(notice, "You qualify for free delivery", "Orders over " + threshold + " ex VAT are delivered free.");
} else {
fillNotice(
notice,
"Free delivery on orders over " + threshold + " ex VAT",
"Add " + money(remaining) + " (ex VAT) more to your basket to qualify.",
);
}
if (!CONFIG.minicartDeliveryRow || !table) return;
const row = ensureDeliveryRow(table);
show(row, true);
setText(row.querySelector(".cart_delivery"), qualifies ? "FREE" : money(delivery));
// Recalculate from $mini_cart every time (never from the DOM) so it can't double-add
const vat = round2(cart.total - cart.goods + delivery * CONFIG.vatRate);
const total = round2(cart.total + delivery * (1 + CONFIG.vatRate));
setText(table.querySelector(".cart_tax"), money(vat));
setText(table.querySelector(".cart_total"), money(total));
});
};
// ---------- Cart page ----------
const sumCartLines = (container) => {
const lines = container.querySelectorAll(".items-list .item_line_total");
if (!lines.length) return null;
let sum = 0;
lines.forEach((el) => {
sum += toNum(el.textContent);
});
return round2(sum);
};
const buildCartNotice = () => {
const goal = moneyShort(CONFIG.freeThreshold);
const row = document.createElement("div");
row.className = "row cart-delivery-row";
row.innerHTML =
'' +
'
' +
'
' +
truckIcon +
"" +
'
' +
'
' +
'
' +
'' +
'' +
"
" +
'
' +
"
" +
'
' +
'
' +
'' +
"
" +
'
' +
goal +
"" +
"
" +
"
" +
"
" +
"
";
return row;
};
const updateCart = () => {
if (!document.body.classList.contains("ex-cart")) return;
document.querySelectorAll(".cartproductlines_container").forEach((container) => {
let row = container.querySelector(".cart-delivery-row");
if (!row) {
row = buildCartNotice();
const main = container.querySelector(".row.main");
if (main) {
main.insertAdjacentElement("afterend", row);
} else {
container.appendChild(row);
}
}
const cart = readCart();
const goods = cart ? cart.goods : sumCartLines(container);
if (goods == null || goods <= 0) {
show(row, false);
return;
}
show(row, true);
const notice = row.querySelector(".delivery-notice");
const threshold = moneyShort(CONFIG.freeThreshold);
const remaining = round2(CONFIG.freeThreshold - goods);
const qualifies = remaining <= 0;
const pct = Math.min(goods / CONFIG.freeThreshold, 1) * 100;
notice.classList.toggle("is-qualified", qualifies);
if (qualifies) {
fillNotice(notice, "You qualify for free delivery", "Orders over " + threshold + " ex VAT are delivered free.");
setText(notice.querySelector(".delivery-notice__pill"), "Free delivery");
} else {
fillNotice(
notice,
"Free delivery on orders over " + threshold + " ex VAT",
"You’re " +
money(remaining) +
" (ex VAT) away from free delivery. Delivery under " +
threshold +
" is " +
money(CONFIG.charge) +
" ex VAT (" +
money(incVat(CONFIG.charge)) +
" inc VAT).",
);
setText(notice.querySelector(".delivery-notice__pill"), money(remaining) + " to go");
}
const fill = notice.querySelector(".delivery-notice__fill");
const width = Number(pct.toFixed(1)) + "%";
if (fill.style.width !== width) fill.style.width = width;
const bar = notice.querySelector(".delivery-notice__bar");
const now = String(Math.min(goods, CONFIG.freeThreshold));
if (bar.getAttribute("aria-valuenow") !== now) {
bar.setAttribute("aria-valuenow", now);
bar.setAttribute("aria-valuetext", qualifies ? "Free delivery reached" : money(remaining) + " to go");
}
});
};
// ---------- wiring ----------
const run = () => {
updatePdp();
updateMinicart();
updateCart();
};
let queued = false;
const schedule = () => {
if (queued) return;
queued = true;
requestAnimationFrame(() => {
queued = false;
run();
});
};
const init = () => {
run();
// EvoX cart updates (jQuery-triggered or native)
if (window.jQuery) {
window.jQuery(document).on("cart-total-update", schedule);
window.jQuery(window).on("cart-total-update", schedule);
}
document.addEventListener("cart-total-update", schedule);
window.addEventListener("cart-total-update", schedule);
// Re-apply if EvoX re-renders the buy box or mini basket / overwrites totals
new MutationObserver(schedule).observe(document.body, {
childList: true,
subtree: true,
characterData: true,
});
};
if (document.readyState === "loading") {
document.addEventListener("DOMContentLoaded", init);
} else {
init();
}
})();