70 lines
3.1 KiB
Plaintext
70 lines
3.1 KiB
Plaintext
{% doc %}
|
|
Merges duplicate cart lines (same variant, same purchase type: one-time or the same
|
|
subscription plan) into a single line with the summed quantity. Duplicates appear when two
|
|
"add" requests for the same product reach Shopify at the same time, or when apps add hidden
|
|
line properties. Free-gift lines ($0 or _kv_gift) are never touched.
|
|
Exposes window.seasonsMergeCartLines() -> Promise<boolean> (true when something was merged).
|
|
|
|
@example
|
|
{% render 'cart-merge-lines' %}
|
|
{% enddoc %}
|
|
<script nolazy>
|
|
(function () {
|
|
if (window.seasonsMergeCartLines) return;
|
|
var root = (window.Shopify && window.Shopify.routes && window.Shopify.routes.root) || '/';
|
|
var running = null;
|
|
var isGift = function (item) {
|
|
var props = item.properties || {};
|
|
return item.final_price === 0 || Object.keys(props).some(function (k) { return /gift|regalo/i.test(k); });
|
|
};
|
|
var visibleProps = function (item) {
|
|
var props = item.properties || {}, out = {};
|
|
Object.keys(props).sort().forEach(function (k) { if (k.charAt(0) !== '_') out[k] = props[k]; });
|
|
return JSON.stringify(out);
|
|
};
|
|
window.seasonsMergeCartLines = function () {
|
|
if (running) return running;
|
|
running = fetch(root + 'cart.js', { headers: { Accept: 'application/json' } })
|
|
.then(function (r) { return r.json(); })
|
|
.then(function (cart) {
|
|
var groups = {};
|
|
(cart.items || []).forEach(function (item) {
|
|
if (isGift(item)) return;
|
|
var plan = item.selling_plan_allocation && item.selling_plan_allocation.selling_plan ? item.selling_plan_allocation.selling_plan.id : 'once';
|
|
var id = item.variant_id + '|' + plan + '|' + visibleProps(item);
|
|
(groups[id] = groups[id] || []).push(item);
|
|
});
|
|
var updates = {}, merged = false;
|
|
Object.keys(groups).forEach(function (id) {
|
|
var lines = groups[id];
|
|
if (lines.length < 2) return;
|
|
merged = true;
|
|
var total = lines.reduce(function (n, l) { return n + l.quantity; }, 0);
|
|
updates[lines[0].key] = total;
|
|
lines.slice(1).forEach(function (l) { updates[l.key] = 0; });
|
|
});
|
|
if (!merged) return false;
|
|
return fetch(root + 'cart/update.js', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json', Accept: 'application/json' },
|
|
body: JSON.stringify({ updates: updates })
|
|
}).then(function (r) { return r.ok; });
|
|
})
|
|
.catch(function () { return false; })
|
|
.finally(function () { running = null; });
|
|
return running;
|
|
};
|
|
// /cart page: merge on load and reload once if something changed.
|
|
if (document.querySelector('.cart-page__items')) {
|
|
window.seasonsMergeCartLines().then(function (merged) {
|
|
if (merged && !sessionStorage.getItem('seasonsMerged')) {
|
|
try { sessionStorage.setItem('seasonsMerged', '1'); } catch (e) {}
|
|
window.location.reload();
|
|
} else {
|
|
try { sessionStorage.removeItem('seasonsMerged'); } catch (e) {}
|
|
}
|
|
});
|
|
}
|
|
})();
|
|
</script>
|