{% comment %} Code for Nutrtion Facts app by ScreenStaring. For support email us at help@screenstaring.com Add this file to your theme's snippets directory as product-label.liquid You must also add the itsgot-product-label-embed-code.liquid file to your theme's snippets directory. This file will render a product's label. If the product has variants then it will show the labels for the selected variant. If the variant changes it will show its label, if any. You can forgo this functionality and display a seperate dropdown using the display_variant_dropdown: option. Or use a dropdown that you've created via the use_variant_dropdown: option and providing a CSS selctor that can be use to retrieve it. See below examples. Usage: {% render "itsgot-product-label", product: product %} Show a dropdown that allows the variant to be selected: {% render "itsgot-product-label", product: product, display_variant_dropdown: true %} The dropdown will be rendered with a DOM ID of itsgot-variant-select {% render "itsgot-product-label", product: product, use_variant_dropdown: "#my_dropdown" %} {% endcomment %} {% assign label = product.metafields.itsgot.label %} {% if label.bundled_products.size > 0 %} {% assign have_label = true %} {% render "product-label-bundle", label: label, use_bundle_dropdown: use_bundle_dropdown %} {% elsif product.has_only_default_variant %} {% comment %}New style, metafields are on the variant{% endcomment %} {% if product.variants[0].metafields.itsgot.labels.value.size > 0 %} {% assign have_label = true %} {% assign labels = product.variants[0].metafields.itsgot.labels.value %} {% for l in labels %} {% render "itsgot-product-label-embed-code", user: l.user, label: l.id, variant: product.variants[0].id, variant_selected: true, locale: l.locale, min_screen_width: l.min_screen_width, max_screen_width: l.max_screen_width, data: l.data %} {% endfor %} {% else %} {% comment %}We use capture to convert the id to a string{% endcomment %} {% capture product_id %}{{ product.id }}{% endcapture %} {% if label.labels.size > 0 %} {% assign have_label = true %} {% for l in label.labels %} {% render "itsgot-product-label-embed-code", user: label.user, label: l.id, locale: l.locale, min_screen_width: l.min_screen_width, max_screen_width: l.max_screen_width, label_number: l.number, data: label.data[product_id] %} {% endfor %} {% elsif label.product %} {% assign have_label = true %} {% render "itsgot-product-label-embed-code", user: label.user, product_id: label.product, data: label.data[product_id] %} {% endif %} {% endif %} {% else %} {% assign variant_with_label = false %} {% for variant in product.variants %} {% capture variant_id %}{{ variant.id }}{% endcapture %} {% if label.variants.labels[variant_id].size > 0 or label.variants[variant_id] %} {% assign variant_with_label = true %} {% break %} {% endif %} {% endfor %} {% if display_variant_dropdown and variant_with_label %}
{% elsif use_variant_dropdown != blank %} {% else %} {% comment %}Can't rely on location.href because this may be called before it changes so we do this on every change{% endcomment %} {% endif %} {% for variant in product.variants %} {% comment %}New style, metafields are on the variant{% endcomment %} {% if variant.metafields.itsgot.labels.value.size > 0 %} {% assign have_label = true %} {% for l in variant.metafields.itsgot.labels.value %} {% render "itsgot-product-label-embed-code", user: l.user, label: l.id, variant: variant.id, variant_selected: variant.selected, locale: l.locale, min_screen_width: l.min_screen_width, max_screen_width: l.max_screen_width, data: l.data %} {% endfor %} {% else %} {% comment %}Need this to use the id as a hash key{% endcomment %} {% capture variant_id %}{{ variant.id }}{% endcapture %} {% if label.variants.labels[variant_id].size > 0 %} {% assign have_label = true %} {% for l in label.variants.labels[variant_id] %} {% render "itsgot-product-label-embed-code", user: label.user, label: l.id, locale: l.locale, min_screen_width: l.min_screen_width, max_screen_width: l.max_screen_width, label_number: l.number, variant: variant.id, variant_selected: variant_selected, data: label.data[variant_id] %} {% endfor %} {% elsif label.variants[variant_id] %} {% assign have_label = true %} {% render "itsgot-product-label-embed-code", user: label.user, product_id: label.variants[variant_id], variant: variant_id, variant_selected: variant_selected, data: label.data[variant_id] %} {% endif %} {% endif %} {% endfor %} {% endif %} {% if have_label %} {% endif %}