276 lines
10 KiB
Plaintext
276 lines
10 KiB
Plaintext
{% doc %}
|
|
Renders a collection product card. Hover shows the second image plus add to cart and buy now.
|
|
|
|
@param {product} product - Product to display
|
|
@param {string} [form_suffix] - Keeps the product form id unique
|
|
|
|
@example
|
|
{% render 'collection-card', product: product, form_suffix: section.id %}
|
|
{% enddoc %}
|
|
|
|
{% liquid
|
|
assign variant = product.selected_or_first_available_variant
|
|
assign card_image = product.featured_image
|
|
assign hover_image = blank
|
|
for image in product.images offset: 1 limit: 1
|
|
assign hover_image = image
|
|
endfor
|
|
assign image_alt = card_image.alt | default: product.title
|
|
|
|
assign subtitle = product.metafields.custom.subtitulo.value | default: product.metafields.custom.subtitulo
|
|
if subtitle == blank
|
|
assign subtitle = product.metafields.custom.subtitle.value | default: product.metafields.custom.subtitle
|
|
endif
|
|
if subtitle == blank
|
|
assign subtitle = product.metafields.custom.presentacion.value | default: product.metafields.custom.presentacion
|
|
endif
|
|
if subtitle == blank and variant.title != 'Default Title'
|
|
assign subtitle = variant.title
|
|
endif
|
|
|
|
assign skin_label = product.metafields.custom.tipo_de_piel.value | default: product.metafields.custom.tipo_de_piel
|
|
if skin_label == blank
|
|
assign skin_label = product.metafields.custom.skin_type.value | default: product.metafields.custom.skin_type
|
|
endif
|
|
|
|
assign badge_best = false
|
|
assign badge_recommended = false
|
|
for tag in product.tags
|
|
assign tag_label = tag | downcase
|
|
if tag_label contains 'vendido' or tag_label contains 'bestseller'
|
|
assign badge_best = true
|
|
endif
|
|
if tag_label contains 'recomend'
|
|
assign badge_recommended = true
|
|
endif
|
|
if skin_label == blank and tag_label contains 'piel'
|
|
assign skin_label = tag
|
|
endif
|
|
endfor
|
|
|
|
assign rating_value = 0
|
|
assign rating_count = 0
|
|
if product.metafields.reviews.rating.value != blank
|
|
assign rating_value = product.metafields.reviews.rating.value.rating | round
|
|
endif
|
|
if product.metafields.reviews.rating_count.value != blank
|
|
assign rating_count = product.metafields.reviews.rating_count.value
|
|
endif
|
|
|
|
assign show_compare = false
|
|
if variant.compare_at_price > variant.price
|
|
assign show_compare = true
|
|
endif
|
|
|
|
assign form_id = 'collection-card-' | append: product.id
|
|
if form_suffix != blank
|
|
assign form_id = form_id | append: '-' | append: form_suffix
|
|
endif
|
|
%}
|
|
|
|
<article class="collection-card">
|
|
<div class="collection-card__media">
|
|
<a class="collection-card__link" href="{{ product.url }}">
|
|
{% if card_image %}
|
|
{{
|
|
card_image
|
|
| image_url: width: 900
|
|
| image_tag:
|
|
alt: image_alt,
|
|
widths: '186, 372, 560, 800',
|
|
sizes: '(max-width: 767px) 46vw, 33vw',
|
|
class: 'collection-card__image',
|
|
loading: 'lazy'
|
|
}}
|
|
{% if hover_image != blank %}
|
|
{{
|
|
hover_image
|
|
| image_url: width: 900
|
|
| image_tag:
|
|
alt: '',
|
|
widths: '186, 372, 560, 800',
|
|
sizes: '(max-width: 767px) 46vw, 33vw',
|
|
class: 'collection-card__image collection-card__image--hover',
|
|
loading: 'lazy'
|
|
}}
|
|
{% endif %}
|
|
{% else %}
|
|
<span class="collection-card__image collection-card__image--empty" aria-hidden="true"></span>
|
|
{% endif %}
|
|
</a>
|
|
|
|
<div class="collection-card__badges">
|
|
{% if badge_best %}
|
|
<span class="collection-card__badge collection-card__badge--best">{{ 'bestsellers.badge' | t }}</span>
|
|
{% endif %}
|
|
{% if badge_recommended %}
|
|
<span class="collection-card__badge collection-card__badge--best">{{ 'collection_page.recommended' | t }}</span>
|
|
{% endif %}
|
|
{% if skin_label != blank %}
|
|
<span class="collection-card__badge collection-card__badge--skin">{{ skin_label | escape }}</span>
|
|
{% endif %}
|
|
</div>
|
|
|
|
<button
|
|
class="collection-card__wish"
|
|
type="button"
|
|
data-wishlist
|
|
data-product-id="{{ product.id }}"
|
|
data-product-handle="{{ product.handle }}"
|
|
aria-pressed="false"
|
|
aria-label="{{ 'bestsellers.save' | t: product: product.title }}"
|
|
data-label-save="{{ 'bestsellers.save' | t: product: product.title }}"
|
|
data-label-saved="{{ 'bestsellers.saved' | t: product: product.title }}"
|
|
>
|
|
<svg width="25" height="25" viewBox="0 0 25 25" aria-hidden="true" focusable="false">
|
|
<path d="M17.2324 3.90625C14.0625 3.90625 12.5 7.03125 12.5 7.03125C12.5 7.03125 10.9375 3.90625 7.76758 3.90625C5.19141 3.90625 3.15137 6.06152 3.125 8.6333C3.07129 13.9717 7.35986 17.7681 12.0605 20.9585C12.1901 21.0467 12.3433 21.0938 12.5 21.0938C12.6567 21.0938 12.8099 21.0467 12.9395 20.9585C17.6396 17.7681 21.9282 13.9717 21.875 8.6333C21.8486 6.06152 19.8086 3.90625 17.2324 3.90625Z" fill="none" stroke="black" stroke-linecap="round" stroke-linejoin="round"/>
|
|
</svg>
|
|
</button>
|
|
|
|
<form
|
|
id="{{ form_id }}"
|
|
class="collection-card__actions"
|
|
action="{{ routes.cart_add_url }}"
|
|
method="post"
|
|
data-collection-card
|
|
>
|
|
<input type="hidden" name="id" value="{{ variant.id }}">
|
|
<input type="hidden" name="quantity" value="1">
|
|
<button class="collection-card__add" type="submit" {% unless variant.available %}disabled{% endunless %}>
|
|
{% if variant.available %}
|
|
{{ 'bestsellers.add_to_cart' | t }}
|
|
{% else %}
|
|
{{ 'bestsellers.sold_out' | t }}
|
|
{% endif %}
|
|
</button>
|
|
<button class="collection-card__buy" type="submit" data-buy {% unless variant.available %}disabled{% endunless %}>
|
|
{{ 'bestsellers.buy_now' | t }}
|
|
</button>
|
|
</form>
|
|
</div>
|
|
|
|
<div class="collection-card__info">
|
|
<a class="collection-card__title" href="{{ product.url }}">{{ product.title | escape }}</a>
|
|
{% if subtitle != blank %}
|
|
<p class="collection-card__subtitle">{{ subtitle | escape }}</p>
|
|
{% endif %}
|
|
{% if rating_count > 0 %}
|
|
<div class="collection-card__rating">
|
|
<span class="collection-card__stars" aria-hidden="true">
|
|
{% for star in (1..5) %}
|
|
<svg class="collection-card__star{% if star > rating_value %} is-off{% endif %}" width="14" height="14" viewBox="0 0 14 14" focusable="false">
|
|
<path d="M7 1.2l1.45 3.22 3.5.4-2.62 2.38.74 3.46L7 8.95 3.93 10.66l.74-3.46L2.05 4.82l3.5-.4L7 1.2z" fill="currentColor"/>
|
|
</svg>
|
|
{% endfor %}
|
|
</span>
|
|
<p class="collection-card__reviews">{{ 'bestsellers.reviews' | t: count: rating_count }}</p>
|
|
</div>
|
|
{% endif %}
|
|
<p class="collection-card__prices">
|
|
<span class="collection-card__price">{{ variant.price | money_without_trailing_zeros }}</span>
|
|
{% if show_compare %}
|
|
<s class="collection-card__compare">{{ variant.compare_at_price | money_without_trailing_zeros }}</s>
|
|
{% endif %}
|
|
</p>
|
|
</div>
|
|
</article>
|
|
|
|
{% javascript %}
|
|
(() => {
|
|
if (window.__seasonCollectionCard) return;
|
|
window.__seasonCollectionCard = true;
|
|
|
|
const wishlistKey = 'seasons-wishlist';
|
|
|
|
const readWishlist = () => {
|
|
try {
|
|
const stored = JSON.parse(window.localStorage.getItem(wishlistKey));
|
|
if (!Array.isArray(stored)) return [];
|
|
return stored.map((item) => {
|
|
if (item && typeof item === 'object') return { id: String(item.id || ''), handle: String(item.handle || '') };
|
|
return { id: String(item), handle: '' };
|
|
}).filter((item) => item.id);
|
|
} catch (error) {
|
|
return [];
|
|
}
|
|
};
|
|
|
|
const applyWishlist = () => {
|
|
const saved = readWishlist().map((item) => item.id);
|
|
document.querySelectorAll('.collection-card [data-wishlist]').forEach((button) => {
|
|
const active = saved.includes(String(button.dataset.productId));
|
|
button.classList.toggle('is-active', active);
|
|
button.setAttribute('aria-pressed', active ? 'true' : 'false');
|
|
const label = active ? button.dataset.labelSaved : button.dataset.labelSave;
|
|
if (label) button.setAttribute('aria-label', label);
|
|
});
|
|
};
|
|
|
|
applyWishlist();
|
|
document.addEventListener('seasons:wishlist', applyWishlist);
|
|
|
|
document.addEventListener('click', (event) => {
|
|
const wish = event.target.closest('.collection-card [data-wishlist]');
|
|
if (!wish) return;
|
|
const id = String(wish.dataset.productId || '');
|
|
const handle = String(wish.dataset.productHandle || '');
|
|
if (!id) return;
|
|
const saved = readWishlist();
|
|
const next = saved.some((item) => item.id === id)
|
|
? saved.filter((item) => item.id !== id)
|
|
: saved.concat({ id, handle });
|
|
window.localStorage.setItem(wishlistKey, JSON.stringify(next));
|
|
document.dispatchEvent(new CustomEvent('seasons:wishlist'));
|
|
applyWishlist();
|
|
});
|
|
|
|
document.addEventListener('submit', async (event) => {
|
|
const form = event.target;
|
|
if (!(form instanceof HTMLFormElement) || !form.hasAttribute('data-collection-card')) return;
|
|
if (event.defaultPrevented) return;
|
|
|
|
event.preventDefault();
|
|
const button = event.submitter instanceof HTMLButtonElement ? event.submitter : form.querySelector('[type="submit"]');
|
|
const variantId = form.querySelector('[name="id"]')?.value;
|
|
if (!button || button.disabled || !variantId) return;
|
|
|
|
const buttons = [...form.querySelectorAll('button')];
|
|
buttons.forEach((item) => {
|
|
item.disabled = true;
|
|
});
|
|
const buyNow = button.hasAttribute('data-buy');
|
|
|
|
try {
|
|
const addResponse = await fetch(`${form.action}.js`, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
Accept: 'application/json',
|
|
},
|
|
body: JSON.stringify({ id: Number(variantId), quantity: 1 }),
|
|
});
|
|
|
|
if (!addResponse.ok) {
|
|
form.submit();
|
|
return;
|
|
}
|
|
|
|
if (buyNow) {
|
|
window.location.href = `${window.Shopify?.routes?.root || '/'}checkout`;
|
|
return;
|
|
}
|
|
|
|
document.dispatchEvent(new CustomEvent('season:cart:open'));
|
|
} catch (error) {
|
|
form.submit();
|
|
} finally {
|
|
if (!buyNow) {
|
|
buttons.forEach((item) => {
|
|
item.disabled = false;
|
|
});
|
|
}
|
|
}
|
|
});
|
|
})();
|
|
{% endjavascript %}
|