/* Build-a-Basket cart handoff for northwoodsconfections.com
Paste into Ecwid admin: Website → (your site) → Settings → Custom code → JavaScript (head or body).
The basket builder sends people here with a URL like
https://northwoodsconfections.com/#mnbasket=873827244:1,833386305:2
(Ecwid product id : quantity, comma separated). This script waits for the Ecwid
storefront to be ready, adds each line to the cart one at a time, clears the
hash so a refresh doesn't add everything twice, then opens the cart page.
It does nothing at all unless that exact #mnbasket= hash is present. */
(function () {
var m = /[#&]mnbasket=([0-9:,]+)/.exec(location.hash);
if (!m) return;
var lines = m[1].split(",").map(function (s) {
var p = s.split(":"), id = parseInt(p[0], 10), qty = parseInt(p[1] || "1", 10);
return (id > 0 && qty > 0 && qty <= 50) ? { id: id, quantity: qty } : null;
}).filter(Boolean).slice(0, 60);
if (!lines.length) return;
// Drop the hash right away so a reload or back-button never re-adds the basket.
try { history.replaceState(null, "", location.pathname + location.search); } catch (e) {}
var banner = document.createElement("div");
banner.setAttribute("role", "status");
banner.style.cssText = "position:fixed;left:50%;top:16px;transform:translateX(-50%);z-index:99999;background:#1f5a46;color:#fff;font:700 15px/1.3 -apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;padding:12px 20px;border-radius:999px;box-shadow:0 10px 30px rgba(0,0,0,.25);max-width:calc(100% - 32px)";
banner.textContent = "Adding your Minnesota basket to the cart…";
document.addEventListener("DOMContentLoaded", function () { document.body.appendChild(banner); });
if (document.body) document.body.appendChild(banner);
function done(added, failed) {
banner.textContent = failed
? "Added " + added + " of " + (added + failed) + " items. A few are no longer available online."
: "Your basket is in the cart.";
setTimeout(function () {
try { Ecwid.openPage("cart"); } catch (e) { location.href = "/cart"; }
setTimeout(function () { banner.remove(); }, 2500);
}, 700);
}
function addAll() {
var i = 0, added = 0, failed = 0;
(function next() {
if (i >= lines.length) return done(added, failed);
var line = lines[i++];
Ecwid.Cart.addProduct({
id: line.id,
quantity: line.quantity,
callback: function (success) { if (success) added++; else failed++; next(); }
});
})();
}
function start() {
if (window.Ecwid && Ecwid.Cart && typeof Ecwid.Cart.addProduct === "function") addAll();
else setTimeout(start, 300);
}
if (window.Ecwid && Ecwid.OnAPILoaded) Ecwid.OnAPILoaded.add(start);
else window.addEventListener("load", start);
})();