484 lines
17 KiB
Plaintext
484 lines
17 KiB
Plaintext
{%- liquid
|
|
assign free_shipping = section.settings.free_shipping | default: 699
|
|
assign threshold = free_shipping | times: 100
|
|
assign progress = 0
|
|
assign remaining = 0
|
|
assign unlocked = false
|
|
|
|
if threshold > 0
|
|
if cart.total_price >= threshold
|
|
assign progress = 100
|
|
assign unlocked = true
|
|
else
|
|
assign remaining = threshold | minus: cart.total_price
|
|
assign progress = cart.total_price | times: 100 | divided_by: threshold
|
|
endif
|
|
endif
|
|
|
|
assign rec_collection = section.settings.recommendations
|
|
if rec_collection == blank
|
|
assign rec_collection = collections.all
|
|
endif
|
|
|
|
assign shown = 0
|
|
capture rec_items
|
|
if rec_collection != blank
|
|
for product in rec_collection.products limit: 24
|
|
if shown >= 4
|
|
break
|
|
endif
|
|
|
|
assign already_in_cart = false
|
|
for item in cart.items
|
|
if item.product_id == product.id
|
|
assign already_in_cart = true
|
|
endif
|
|
endfor
|
|
if already_in_cart
|
|
continue
|
|
endif
|
|
|
|
assign rec_variant = product.selected_or_first_available_variant
|
|
if rec_variant == blank
|
|
continue
|
|
endif
|
|
unless rec_variant.available
|
|
continue
|
|
endunless
|
|
|
|
assign shown = shown | plus: 1
|
|
assign add_label = 'cart_drawer.add_short' | t
|
|
|
|
echo '<article class="cart-drawer__rec"><a class="cart-drawer__thumb cart-drawer__thumb--rec" href="'
|
|
echo product.url | escape
|
|
echo '">'
|
|
if product.featured_image
|
|
echo product.featured_image | image_url: width: 160 | image_tag: loading: 'lazy', alt: product.title, class: 'cart-drawer__img'
|
|
endif
|
|
echo '</a><div class="cart-drawer__rec-body"><a class="cart-drawer__rec-title" href="'
|
|
echo product.url | escape
|
|
echo '">'
|
|
echo product.title | escape
|
|
echo '</a><p class="cart-drawer__price">'
|
|
echo rec_variant.price | money_without_trailing_zeros
|
|
echo '</p><button class="cart-drawer__add" type="button" data-cart-drawer-add data-variant-id="'
|
|
echo rec_variant.id
|
|
echo '">'
|
|
echo add_label | escape
|
|
echo '<svg width="14" height="14" viewBox="0 0 14 14" aria-hidden="true" focusable="false"><path d="M2 7h10M8 3l4 4-4 4" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"/></svg></button></div></article>'
|
|
endfor
|
|
endif
|
|
endcapture
|
|
-%}
|
|
|
|
<div
|
|
id="cart-drawer"
|
|
class="cart-drawer"
|
|
data-cart-drawer
|
|
data-section-id="{{ section.id }}"
|
|
data-add-url="{{ routes.cart_add_url }}.js"
|
|
data-change-url="{{ routes.cart_change_url }}.js"
|
|
aria-hidden="true"
|
|
>
|
|
<button class="cart-drawer__backdrop" type="button" data-cart-drawer-close aria-label="{{ 'cart_drawer.close' | t }}"></button>
|
|
|
|
<div
|
|
class="cart-drawer__panel"
|
|
data-cart-drawer-panel
|
|
role="dialog"
|
|
aria-modal="true"
|
|
aria-labelledby="cart-drawer-title"
|
|
>
|
|
<div class="cart-drawer__layout" data-cart-drawer-content data-item-count="{{ cart.item_count }}">
|
|
<div class="cart-drawer__main">
|
|
<div class="cart-drawer__head">
|
|
<h2 id="cart-drawer-title" class="cart-drawer__heading">{{ 'cart_drawer.title' | t }}</h2>
|
|
<button class="cart-drawer__close" type="button" data-cart-drawer-close aria-label="{{ 'cart_drawer.close' | t }}">
|
|
<svg width="14" height="14" viewBox="0 0 14 14" aria-hidden="true" focusable="false">
|
|
<path d="M2 2l10 10M12 2L2 12" fill="none" stroke="currentColor" stroke-width="1.4" stroke-linecap="round"/>
|
|
</svg>
|
|
</button>
|
|
</div>
|
|
|
|
{% if threshold > 0 %}
|
|
<div class="cart-drawer__shipping">
|
|
<p>
|
|
{% if unlocked %}
|
|
{{ 'cart_drawer.unlocked' | t }}
|
|
{% else %}
|
|
{{ 'cart_drawer.remaining_before' | t }}
|
|
{{ remaining | money_without_trailing_zeros }}
|
|
{{ 'cart_drawer.remaining_after' | t }}
|
|
<span>{{ 'cart_drawer.free' | t }}</span>
|
|
{% endif %}
|
|
</p>
|
|
<div
|
|
class="cart-drawer__bar"
|
|
style="--progress: {{ progress }}%;"
|
|
role="progressbar"
|
|
aria-valuemin="0"
|
|
aria-valuemax="100"
|
|
aria-valuenow="{{ progress }}"
|
|
aria-label="{{ 'cart_drawer.shipping_progress' | t }}"
|
|
>
|
|
<span class="cart-drawer__bar-fill"></span>
|
|
</div>
|
|
</div>
|
|
{% endif %}
|
|
|
|
<div class="cart-drawer__items">
|
|
{% if cart.item_count == 0 %}
|
|
<p class="cart-drawer__empty">{{ 'cart_drawer.empty' | t }}</p>
|
|
{% else %}
|
|
{% for item in cart.items %}
|
|
<article class="cart-drawer__line">
|
|
<a class="cart-drawer__thumb" href="{{ item.url }}">
|
|
{% if item.image %}
|
|
{{ item.image | image_url: width: 180 | image_tag: loading: 'lazy', alt: item.product.title, class: 'cart-drawer__img' }}
|
|
{% endif %}
|
|
</a>
|
|
<div class="cart-drawer__line-body">
|
|
<a class="cart-drawer__line-title" href="{{ item.url }}">{{ item.product.title }}</a>
|
|
{% unless item.variant.title == 'Default Title' %}
|
|
<p class="cart-drawer__variant">{{ item.variant.title }}</p>
|
|
{% endunless %}
|
|
<p class="cart-drawer__price">{{ item.final_line_price | money_without_trailing_zeros }}</p>
|
|
<div class="cart-drawer__qty">
|
|
<button
|
|
type="button"
|
|
data-cart-drawer-qty
|
|
data-line-key="{{ item.key | escape }}"
|
|
data-quantity="{{ item.quantity | minus: 1 }}"
|
|
{% if item.quantity <= 1 %}
|
|
disabled
|
|
{% endif %}
|
|
aria-label="{{ 'cart_drawer.decrease' | t }}"
|
|
>
|
|
−
|
|
</button>
|
|
<span>{{ item.quantity }}</span>
|
|
<button
|
|
type="button"
|
|
data-cart-drawer-qty
|
|
data-line-key="{{ item.key | escape }}"
|
|
data-quantity="{{ item.quantity | plus: 1 }}"
|
|
aria-label="{{ 'cart_drawer.increase' | t }}"
|
|
>
|
|
+
|
|
</button>
|
|
</div>
|
|
</div>
|
|
<button
|
|
class="cart-drawer__remove"
|
|
type="button"
|
|
data-cart-drawer-qty
|
|
data-line-key="{{ item.key | escape }}"
|
|
data-quantity="0"
|
|
aria-label="{{ 'cart_drawer.remove' | t }}"
|
|
>
|
|
<svg width="16" height="16" viewBox="0 0 16 16" aria-hidden="true" focusable="false">
|
|
<path d="M2.5 4h11M6.5 4V2.5h3V4M4 4l.6 9.2a1 1 0 0 0 1 .8h4.8a1 1 0 0 0 1-.8L12 4" fill="none" stroke="currentColor" stroke-linecap="round" stroke-linejoin="round"/>
|
|
</svg>
|
|
</button>
|
|
</article>
|
|
{% endfor %}
|
|
{% endif %}
|
|
</div>
|
|
|
|
<div class="cart-drawer__footer">
|
|
<p class="cart-drawer__subtotal">
|
|
<span>{{ 'cart_drawer.subtotal' | t }}</span>
|
|
<strong>{{ cart.total_price | money_without_trailing_zeros }} {{ cart.currency.iso_code }}</strong>
|
|
</p>
|
|
<p class="cart-drawer__note">{{ 'cart_drawer.taxes' | t }}</p>
|
|
{% if shown > 0 %}
|
|
<aside class="cart-drawer__recs" aria-label="{{ 'cart_drawer.recommendations' | t }}">
|
|
<h2 class="cart-drawer__heading">{{ 'cart_drawer.recommendations' | t }}</h2>
|
|
<div class="cart-drawer__rec-list">
|
|
{{ rec_items }}
|
|
</div>
|
|
</aside>
|
|
{% endif %}
|
|
<div class="cart-drawer__actions">
|
|
{% if cart.item_count > 0 %}
|
|
<form action="{{ routes.cart_url }}" method="post">
|
|
<button class="cart-drawer__checkout" type="submit" name="checkout">{{ 'cart_drawer.checkout' | t }}</button>
|
|
</form>
|
|
{% else %}
|
|
<button class="cart-drawer__checkout" type="button" disabled>{{ 'cart_drawer.checkout' | t }}</button>
|
|
{% endif %}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{% javascript %}
|
|
(() => {
|
|
if (window.__seasonCartDrawer) return;
|
|
window.__seasonCartDrawer = true;
|
|
|
|
const getDrawer = () => document.querySelector('[data-cart-drawer]');
|
|
let lastFocus = null;
|
|
let requestId = 0;
|
|
|
|
const setCount = (count) => {
|
|
document.querySelectorAll('[data-cart-open]').forEach((button) => {
|
|
const template = button.dataset.cartCountLabel || '';
|
|
if (template) button.setAttribute('aria-label', template.replace('__COUNT__', String(count)));
|
|
|
|
let badge = button.querySelector('[data-cart-count]');
|
|
if (count < 1) {
|
|
badge?.remove();
|
|
return;
|
|
}
|
|
|
|
if (!badge) {
|
|
badge = document.createElement('span');
|
|
badge.className = 'absolute right-0 top-0 flex h-4 min-w-4 items-center justify-center rounded-full bg-brand-mint px-[3px] text-[10px] font-medium leading-none text-black';
|
|
badge.setAttribute('data-cart-count', '');
|
|
button.appendChild(badge);
|
|
}
|
|
|
|
badge.textContent = String(count);
|
|
});
|
|
};
|
|
|
|
const mobileQuery = window.matchMedia('(max-width: 767px)');
|
|
let floaterObserver = null;
|
|
|
|
const parkFloaters = (root) => {
|
|
if (!mobileQuery.matches) return;
|
|
const nodes = root === document.body ? document.body.querySelectorAll('*') : [root, ...root.querySelectorAll('*')];
|
|
nodes.forEach((node) => {
|
|
if (!(node instanceof HTMLElement) || node.closest('[data-cart-drawer]')) return;
|
|
if (node.hasAttribute('data-cart-parked')) return;
|
|
const style = window.getComputedStyle(node);
|
|
if (style.position !== 'fixed') return;
|
|
const rect = node.getBoundingClientRect();
|
|
if (rect.width < 24 || rect.height < 24) return;
|
|
const namedWidget = node.id === 'aitana-fab' || node.id === 'aitana-overlay' || node.id === 'wa-coupon-fab' || node.id === 'wa-coupon-overlay';
|
|
const low = rect.top > window.innerHeight * 0.4;
|
|
const side = rect.left < 160 || rect.right > window.innerWidth - 160;
|
|
if (!namedWidget && (!low || !side)) return;
|
|
node.setAttribute('data-cart-parked', '');
|
|
});
|
|
};
|
|
|
|
const watchFloaters = () => {
|
|
parkFloaters(document.body);
|
|
if (floaterObserver) return;
|
|
floaterObserver = new MutationObserver((records) => {
|
|
if (!document.documentElement.classList.contains('cart-drawer-open')) return;
|
|
records.forEach((record) => {
|
|
record.addedNodes.forEach((node) => {
|
|
if (node instanceof HTMLElement) parkFloaters(node);
|
|
});
|
|
});
|
|
});
|
|
floaterObserver.observe(document.body, { childList: true, subtree: true });
|
|
};
|
|
|
|
const releaseFloaters = () => {
|
|
document.querySelectorAll('[data-cart-parked]').forEach((node) => {
|
|
node.removeAttribute('data-cart-parked');
|
|
});
|
|
};
|
|
|
|
const openDrawer = () => {
|
|
const drawer = getDrawer();
|
|
if (!drawer) return;
|
|
if (!drawer.classList.contains('is-open')) lastFocus = document.activeElement;
|
|
drawer.classList.remove('is-closing');
|
|
drawer.classList.add('is-open');
|
|
drawer.setAttribute('aria-hidden', 'false');
|
|
document.documentElement.classList.add('cart-drawer-open');
|
|
watchFloaters();
|
|
document.querySelectorAll('[data-cart-open]').forEach((button) => {
|
|
button.setAttribute('aria-expanded', 'true');
|
|
});
|
|
drawer.querySelector('.cart-drawer__close')?.focus();
|
|
};
|
|
|
|
const closeDrawer = () => {
|
|
const drawer = getDrawer();
|
|
if (!drawer || !drawer.classList.contains('is-open')) return;
|
|
drawer.classList.remove('is-open');
|
|
drawer.classList.add('is-closing');
|
|
drawer.setAttribute('aria-hidden', 'true');
|
|
document.documentElement.classList.remove('cart-drawer-open');
|
|
releaseFloaters();
|
|
document.querySelectorAll('[data-cart-open]').forEach((button) => {
|
|
button.setAttribute('aria-expanded', 'false');
|
|
});
|
|
|
|
const panel = drawer.querySelector('[data-cart-drawer-panel]');
|
|
const finish = (event) => {
|
|
if (event && event.target !== panel) return;
|
|
if (drawer.classList.contains('is-open')) return;
|
|
drawer.classList.remove('is-closing');
|
|
panel?.removeEventListener('transitionend', finish);
|
|
};
|
|
panel?.addEventListener('transitionend', finish);
|
|
window.setTimeout(finish, 450);
|
|
|
|
if (lastFocus && typeof lastFocus.focus === 'function') lastFocus.focus();
|
|
};
|
|
|
|
const refresh = async (shouldOpen) => {
|
|
const drawer = getDrawer();
|
|
if (!drawer) return;
|
|
const currentId = ++requestId;
|
|
|
|
try {
|
|
const url = new URL(window.location.href);
|
|
url.searchParams.set('section_id', drawer.dataset.sectionId);
|
|
url.hash = '';
|
|
const response = await fetch(url.toString(), { headers: { Accept: 'text/html' } });
|
|
if (!response.ok || currentId !== requestId) return;
|
|
const fresh = new DOMParser().parseFromString(await response.text(), 'text/html').querySelector('[data-cart-drawer-content]');
|
|
const current = getDrawer()?.querySelector('[data-cart-drawer-content]');
|
|
if (!fresh || !current || currentId !== requestId) return;
|
|
current.replaceWith(fresh);
|
|
setCount(Number(fresh.dataset.itemCount || '0'));
|
|
} catch (error) {
|
|
// Keep the cart that is already on the page.
|
|
}
|
|
|
|
if (shouldOpen) openDrawer();
|
|
};
|
|
|
|
const postJson = async (url, payload) => {
|
|
const response = await fetch(url, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Accept: 'application/json',
|
|
},
|
|
body: JSON.stringify(payload),
|
|
});
|
|
return response.ok;
|
|
};
|
|
|
|
document.addEventListener('season:cart:open', () => {
|
|
const drawer = getDrawer();
|
|
drawer?.classList.add('is-busy');
|
|
openDrawer();
|
|
refresh(false).finally(() => {
|
|
getDrawer()?.classList.remove('is-busy');
|
|
});
|
|
});
|
|
|
|
document.addEventListener('keydown', (event) => {
|
|
if (event.key !== 'Escape') return;
|
|
if (!getDrawer()?.classList.contains('is-open')) return;
|
|
event.preventDefault();
|
|
closeDrawer();
|
|
});
|
|
|
|
document.addEventListener('click', async (event) => {
|
|
const opener = event.target.closest('[data-cart-open]');
|
|
if (opener) {
|
|
event.preventDefault();
|
|
openDrawer();
|
|
return;
|
|
}
|
|
|
|
const drawer = getDrawer();
|
|
if (!drawer || !drawer.classList.contains('is-open')) return;
|
|
|
|
if (event.target.closest('[data-cart-drawer-close]')) {
|
|
closeDrawer();
|
|
return;
|
|
}
|
|
|
|
if (drawer.classList.contains('is-busy')) return;
|
|
|
|
const addButton = event.target.closest('[data-cart-drawer-add]');
|
|
if (addButton && drawer.contains(addButton)) {
|
|
const variantId = addButton.dataset.variantId;
|
|
if (!variantId) return;
|
|
drawer.classList.add('is-busy');
|
|
try {
|
|
const ok = await postJson(drawer.dataset.addUrl, { id: Number(variantId), quantity: 1 });
|
|
if (ok) await refresh(false);
|
|
} finally {
|
|
getDrawer()?.classList.remove('is-busy');
|
|
}
|
|
return;
|
|
}
|
|
|
|
const qtyButton = event.target.closest('[data-cart-drawer-qty]');
|
|
if (qtyButton && drawer.contains(qtyButton)) {
|
|
const key = qtyButton.dataset.lineKey;
|
|
const quantity = Number(qtyButton.dataset.quantity);
|
|
if (!key || Number.isNaN(quantity)) return;
|
|
drawer.classList.add('is-busy');
|
|
try {
|
|
const ok = await postJson(drawer.dataset.changeUrl, { id: key, quantity });
|
|
if (ok) await refresh(false);
|
|
} finally {
|
|
getDrawer()?.classList.remove('is-busy');
|
|
}
|
|
}
|
|
});
|
|
|
|
document.addEventListener('submit', async (event) => {
|
|
if (event.defaultPrevented) return;
|
|
const form = event.target;
|
|
if (!(form instanceof HTMLFormElement)) return;
|
|
const action = form.getAttribute('action') || '';
|
|
if (!action.includes('/cart/add')) return;
|
|
if (event.submitter && event.submitter.closest('.shopify-payment-button')) return;
|
|
if (event.submitter && event.submitter.hasAttribute('data-buy')) return;
|
|
|
|
event.preventDefault();
|
|
const drawer = getDrawer();
|
|
drawer?.classList.add('is-busy');
|
|
|
|
try {
|
|
const endpoint = action.includes('.js') ? action : action.replace(/\/?$/, '.js').replace('.js.js', '.js');
|
|
const response = await fetch(endpoint, {
|
|
method: 'POST',
|
|
headers: { Accept: 'application/json' },
|
|
body: new FormData(form),
|
|
});
|
|
if (!response.ok) {
|
|
form.submit();
|
|
return;
|
|
}
|
|
openDrawer();
|
|
await refresh(false);
|
|
} catch (error) {
|
|
form.submit();
|
|
} finally {
|
|
getDrawer()?.classList.remove('is-busy');
|
|
}
|
|
});
|
|
})();
|
|
{% endjavascript %}
|
|
|
|
{% schema %}
|
|
{
|
|
"name": "t:cart_drawer.name",
|
|
"tag": "section",
|
|
"class": "cart-drawer-section",
|
|
"settings": [
|
|
{
|
|
"type": "number",
|
|
"id": "free_shipping",
|
|
"label": "t:cart_drawer.free_shipping",
|
|
"info": "t:cart_drawer.free_shipping_info",
|
|
"default": 699
|
|
},
|
|
{
|
|
"type": "collection",
|
|
"id": "recommendations",
|
|
"label": "t:cart_drawer.recommendations",
|
|
"info": "t:cart_drawer.recommendations_info"
|
|
}
|
|
]
|
|
}
|
|
{% endschema %}
|