Prestige/snippets/product-badges.liquid
2025-02-17 13:02:58 +08:00

119 lines
4.9 KiB
Plaintext

{%- comment -%}
----------------------------------------------------------------------------------------------------------------------
PRODUCT BADGES COMPONENT
----------------------------------------------------------------------------------------------------------------------
This component is used in product listing and product page to render the badges of a given product
********************************************
Supported variables
********************************************
* product: the product to render the badges
* variant: the specific variant to show the badge from
* vertical: if set to true, the badges are outputted vertically
* types: the types of badge to output. Can be "custom", "sold_out" or "discount" (or a combination separated by comma). If nothing is set, all badges are outputted.
* context: can be "product" or "card"
{%- endcomment -%}
{%- liquid
assign badge_types = types | default: 'custom, sold_out, discount' | split: ','
assign variant = variant | default: product.selected_or_first_available_variant
-%}
{%- capture badges -%}
{%- for badge_type in badge_types -%}
{%- assign stripped_badge_type = badge_type | strip -%}
{%- case stripped_badge_type -%}
{%- when 'custom' -%}
{%- assign custom_badges = product.metafields.custom.badges.value -%}
{%- for custom_badge in custom_badges -%}
<span class="badge badge--custom badge--{{ custom_badge | handle }}">{{ custom_badge }}</span>
{%- endfor -%}
{%- when 'sold_out' -%}
{%- if settings.show_sold_out_badge and variant.available == false -%}
<sold-out-badge class="badge badge--sold-out">
{{- 'product.general.sold_out_badge' | t -}}
</sold-out-badge>
{%- endif -%}
{%- when 'discount' -%}
{%- if settings.show_discount -%}
{%- liquid
assign show_on_sale_badge = false
assign variant_price = variant.price
assign variant_compare_at_price = variant.compare_at_price
assign product_price = product.price
assign product_compare_at_price = product.compare_at_price
if variant.metafields.app--168074346497.auto_discounted_price.value
if variant.metafields.app--168074346497.auto_discounted_price.value < variant.price
assign variant_price = variant.metafields.app--168074346497.auto_discounted_price.value
assign variant_compare_at_price = variant.price
if variant.compare_at_price > variant.price
assign variant_compare_at_price = variant.compare_at_price
endif
endif
endif
if product.metafields.app--168074346497.min_auto_discounted_price.value
if product.metafields.app--168074346497.min_auto_discounted_price.value < product.price
assign product_price = product.metafields.app--168074346497.min_auto_discounted_price.value
assign product_compare_at_price = product.price
if product.compare_at_price > product.price
assign product_compare_at_price = product.compare_at_price
endif
endif
endif
if context == 'product'
if variant_compare_at_price > variant_price
assign show_on_sale_badge = true
endif
elsif context == 'card'
if product_compare_at_price > product_price
assign show_on_sale_badge = true
endif
endif
-%}
{%- if product_compare_at_price > product_price -%}
{%- if settings.discount_mode == 'percentage' -%}
{%- assign savings = variant_compare_at_price | minus: variant_price | times: 100.0 | divided_by: variant_compare_at_price | round | append: '%' -%}
{%- else -%}
{%- capture savings -%}{{ variant_compare_at_price | minus: variant_price | money }}{%- endcapture -%}
{%- endif -%}
{%- if show_on_sale_badge -%}
<on-sale-badge class="badge badge--on-sale">
{%- liquid
if context == 'product'
echo 'product.general.discount_badge_html' | t: savings: savings
elsif context == 'card'
if product.compare_at_price_varies or variant_compare_at_price == blank
echo 'product.general.on_sale_badge' | t
else
echo 'product.general.discount_badge_html' | t: savings: savings
endif
endif
-%}
</on-sale-badge>
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endcase -%}
{%- endfor -%}
{%- endcapture -%}
{%- if badges != blank -%}
<badge-list class="badge-list {% if vertical %}badge-list--vertical{% endif %}">
{{- badges -}}
</badge-list>
{%- endif -%}