862 lines
23 KiB
Plaintext
862 lines
23 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' | 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 }}">
|
|
{% 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__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>
|
|
<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 %}
|
|
<a class="cart-drawer__view" href="{{ routes.cart_url }}">{{ 'cart_drawer.view' | t }}</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
{% stylesheet %}
|
|
.shopify-section.cart-drawer-section {
|
|
display: block;
|
|
width: 0;
|
|
height: 0;
|
|
overflow: visible;
|
|
position: static;
|
|
}
|
|
|
|
.cart-drawer {
|
|
position: fixed;
|
|
inset: 0;
|
|
z-index: 80;
|
|
visibility: hidden;
|
|
pointer-events: none;
|
|
}
|
|
|
|
.cart-drawer.is-open,
|
|
.cart-drawer.is-closing {
|
|
visibility: visible;
|
|
}
|
|
|
|
.cart-drawer.is-open {
|
|
pointer-events: auto;
|
|
}
|
|
|
|
.cart-drawer__backdrop {
|
|
position: absolute;
|
|
inset: 0;
|
|
border: 0;
|
|
padding: 0;
|
|
background: rgba(32, 36, 36, 0.46);
|
|
opacity: 0;
|
|
cursor: pointer;
|
|
transition: opacity 0.32s ease;
|
|
}
|
|
|
|
.cart-drawer.is-open .cart-drawer__backdrop {
|
|
opacity: 1;
|
|
}
|
|
|
|
.cart-drawer__panel {
|
|
position: absolute;
|
|
top: 0;
|
|
right: 0;
|
|
display: flex;
|
|
width: min(980px, 100%);
|
|
height: 100%;
|
|
background: #fff;
|
|
color: #000;
|
|
box-shadow: -12px 0 40px rgba(0, 0, 0, 0.12);
|
|
transform: translate3d(100%, 0, 0);
|
|
transition: transform 0.38s cubic-bezier(0.22, 1, 0.36, 1);
|
|
}
|
|
|
|
.cart-drawer.is-open .cart-drawer__panel {
|
|
transform: translate3d(0, 0, 0);
|
|
}
|
|
|
|
.cart-drawer__layout {
|
|
display: grid;
|
|
grid-template-columns: minmax(280px, 380px) minmax(0, 1fr);
|
|
width: 100%;
|
|
min-height: 0;
|
|
}
|
|
|
|
.cart-drawer__layout:not(:has(.cart-drawer__recs)) {
|
|
grid-template-columns: 1fr;
|
|
}
|
|
|
|
.cart-drawer__recs,
|
|
.cart-drawer__items {
|
|
overscroll-behavior: contain;
|
|
}
|
|
|
|
.cart-drawer__recs {
|
|
overflow: auto;
|
|
padding: 28px 24px 32px;
|
|
border-right: 1px solid #ededed;
|
|
}
|
|
|
|
.cart-drawer__main {
|
|
display: flex;
|
|
flex-direction: column;
|
|
min-width: 0;
|
|
min-height: 0;
|
|
padding: 28px 28px 0;
|
|
}
|
|
|
|
.cart-drawer__heading {
|
|
margin: 0;
|
|
font-size: 13px;
|
|
font-weight: 700;
|
|
letter-spacing: 0.08em;
|
|
line-height: 1.3;
|
|
text-transform: uppercase;
|
|
}
|
|
|
|
.cart-drawer__head {
|
|
display: flex;
|
|
align-items: flex-start;
|
|
justify-content: space-between;
|
|
gap: 16px;
|
|
}
|
|
|
|
.cart-drawer__close,
|
|
.cart-drawer__remove,
|
|
.cart-drawer__add,
|
|
.cart-drawer__qty button {
|
|
border: 0;
|
|
background: transparent;
|
|
color: inherit;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.cart-drawer__close {
|
|
display: inline-flex;
|
|
flex: 0 0 auto;
|
|
align-items: center;
|
|
justify-content: center;
|
|
width: 32px;
|
|
height: 32px;
|
|
margin: -6px -6px 0 0;
|
|
padding: 0;
|
|
}
|
|
|
|
.cart-drawer__shipping {
|
|
margin-top: 18px;
|
|
}
|
|
|
|
.cart-drawer__shipping p {
|
|
margin: 0 0 10px;
|
|
font-size: 13px;
|
|
font-weight: 400;
|
|
line-height: 1.4;
|
|
}
|
|
|
|
.cart-drawer__shipping span {
|
|
color: #1aa37a;
|
|
font-weight: 700;
|
|
}
|
|
|
|
.cart-drawer__bar {
|
|
height: 4px;
|
|
overflow: hidden;
|
|
border-radius: 999px;
|
|
background: #ececec;
|
|
}
|
|
|
|
.cart-drawer__bar-fill {
|
|
display: block;
|
|
width: var(--progress);
|
|
height: 100%;
|
|
border-radius: inherit;
|
|
background: #9dd6c7;
|
|
}
|
|
|
|
.cart-drawer__items {
|
|
flex: 1;
|
|
overflow: auto;
|
|
margin-top: 8px;
|
|
}
|
|
|
|
.cart-drawer__empty {
|
|
margin: 48px 0;
|
|
color: #666;
|
|
font-size: 14px;
|
|
font-weight: 400;
|
|
text-align: center;
|
|
}
|
|
|
|
.cart-drawer__line,
|
|
.cart-drawer__rec {
|
|
display: grid;
|
|
align-items: center;
|
|
gap: 14px;
|
|
}
|
|
|
|
.cart-drawer__line {
|
|
grid-template-columns: 72px minmax(0, 1fr) auto;
|
|
padding: 18px 0;
|
|
}
|
|
|
|
.cart-drawer__rec {
|
|
grid-template-columns: 56px minmax(0, 1fr);
|
|
align-items: start;
|
|
padding: 18px 0;
|
|
border-bottom: 1px solid #f1f1f1;
|
|
}
|
|
|
|
.cart-drawer__thumb {
|
|
display: block;
|
|
overflow: hidden;
|
|
width: 72px;
|
|
height: 72px;
|
|
background: #f7f7f7;
|
|
}
|
|
|
|
.cart-drawer__thumb--rec {
|
|
width: 56px;
|
|
height: 64px;
|
|
}
|
|
|
|
.cart-drawer__img {
|
|
display: block;
|
|
width: 100%;
|
|
height: 100%;
|
|
object-fit: contain;
|
|
}
|
|
|
|
.cart-drawer__line-title,
|
|
.cart-drawer__rec-title {
|
|
display: block;
|
|
color: inherit;
|
|
font-size: 14px;
|
|
font-weight: 500;
|
|
line-height: 1.35;
|
|
text-decoration: none;
|
|
}
|
|
|
|
.cart-drawer__rec-title {
|
|
font-size: 13px;
|
|
font-weight: 400;
|
|
}
|
|
|
|
.cart-drawer__variant {
|
|
margin: 2px 0 0;
|
|
color: #777;
|
|
font-size: 12px;
|
|
font-weight: 400;
|
|
}
|
|
|
|
.cart-drawer__price {
|
|
margin: 6px 0 0;
|
|
font-size: 14px;
|
|
font-weight: 600;
|
|
line-height: 1.2;
|
|
}
|
|
|
|
.cart-drawer__add {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
margin-top: 8px;
|
|
padding: 0;
|
|
font-size: 11px;
|
|
font-weight: 500;
|
|
letter-spacing: 0.04em;
|
|
line-height: 1;
|
|
text-transform: uppercase;
|
|
}
|
|
|
|
.cart-drawer__qty {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
height: 32px;
|
|
margin-top: 10px;
|
|
border: 1px solid #e1e1e1;
|
|
border-radius: 4px;
|
|
}
|
|
|
|
.cart-drawer__qty button {
|
|
width: 32px;
|
|
height: 32px;
|
|
padding: 0;
|
|
font-size: 16px;
|
|
line-height: 1;
|
|
}
|
|
|
|
.cart-drawer__qty button:disabled {
|
|
color: #bbb;
|
|
cursor: default;
|
|
}
|
|
|
|
.cart-drawer__qty span {
|
|
min-width: 16px;
|
|
font-size: 14px;
|
|
text-align: center;
|
|
}
|
|
|
|
.cart-drawer__remove {
|
|
display: inline-flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
width: 32px;
|
|
height: 32px;
|
|
padding: 0;
|
|
}
|
|
|
|
.cart-drawer__footer {
|
|
flex: 0 0 auto;
|
|
padding: 16px 0 24px;
|
|
background: #fff;
|
|
}
|
|
|
|
.cart-drawer__subtotal {
|
|
display: flex;
|
|
align-items: baseline;
|
|
justify-content: space-between;
|
|
gap: 16px;
|
|
margin: 0;
|
|
font-size: 16px;
|
|
font-weight: 500;
|
|
}
|
|
|
|
.cart-drawer__subtotal strong {
|
|
font-weight: 600;
|
|
}
|
|
|
|
.cart-drawer__note {
|
|
margin: 8px 0 0;
|
|
color: #6d6d6d;
|
|
font-size: 12px;
|
|
font-weight: 300;
|
|
line-height: 1.4;
|
|
text-align: center;
|
|
}
|
|
|
|
.cart-drawer__actions {
|
|
display: grid;
|
|
grid-template-columns: 1fr 1fr;
|
|
gap: 12px;
|
|
margin-top: 16px;
|
|
}
|
|
|
|
.cart-drawer__actions form {
|
|
display: contents;
|
|
}
|
|
|
|
.cart-drawer__checkout,
|
|
.cart-drawer__view {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
height: 48px;
|
|
border-radius: 999px;
|
|
font-size: 13px;
|
|
font-weight: 600;
|
|
letter-spacing: 0.04em;
|
|
line-height: 1;
|
|
text-align: center;
|
|
text-decoration: none;
|
|
text-transform: uppercase;
|
|
}
|
|
|
|
.cart-drawer__checkout {
|
|
border: 0;
|
|
background: #9dd6c7;
|
|
color: #000;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.cart-drawer__checkout:disabled {
|
|
opacity: 0.45;
|
|
cursor: default;
|
|
}
|
|
|
|
.cart-drawer__view {
|
|
border: 1px solid #111;
|
|
background: #fff;
|
|
color: #000;
|
|
}
|
|
|
|
.cart-drawer.is-busy .cart-drawer__layout {
|
|
pointer-events: none;
|
|
}
|
|
|
|
html.cart-drawer-open,
|
|
html.cart-drawer-open body {
|
|
overflow: hidden;
|
|
}
|
|
|
|
@media (max-width: 767px) {
|
|
.cart-drawer__panel {
|
|
width: 100%;
|
|
}
|
|
|
|
.cart-drawer__layout,
|
|
.cart-drawer__layout:not(:has(.cart-drawer__recs)) {
|
|
display: flex;
|
|
flex-direction: column;
|
|
overflow: auto;
|
|
}
|
|
|
|
.cart-drawer__recs {
|
|
order: 2;
|
|
border-right: 0;
|
|
border-top: 1px solid #ededed;
|
|
}
|
|
|
|
.cart-drawer__main {
|
|
order: 1;
|
|
min-height: 100%;
|
|
padding: 20px 16px 0;
|
|
}
|
|
|
|
.cart-drawer__items {
|
|
overflow: visible;
|
|
}
|
|
|
|
.cart-drawer__footer {
|
|
position: sticky;
|
|
bottom: 0;
|
|
padding-bottom: 20px;
|
|
}
|
|
|
|
.cart-drawer__actions {
|
|
grid-template-columns: 1fr;
|
|
}
|
|
}
|
|
|
|
@media (prefers-reduced-motion: reduce) {
|
|
.cart-drawer__backdrop,
|
|
.cart-drawer__panel {
|
|
transition: none;
|
|
}
|
|
}
|
|
{% endstylesheet %}
|
|
|
|
{% 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 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');
|
|
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');
|
|
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 %}
|