Hi,
I have created a meta field to add some custom text in the label box (over product images). I have this working fine on the collection pages, however on the product pages the below code seems to target products with variations specifically, but I would like this to apply to all products (including Bundle products created with the Shopify Bundles app):
{%- for variant in product.variants -%}
{% if variant.id == current_variant.id and label_text %}
{{ label_text }}
{%- endif -%}
{%- endfor -%}
I have created the following code that works on the collections page - if there is a value in the meta field then replace the label text with the value:
{% if product.metafields.custom.product_label == blank %}
{{ label_text }}
{% elsif product.metafields.custom.product_label %}
{{ product.metafields.custom.product_label | metafield_text }}
{%- endif -%}
I hope the problem is clear? The top code is specifically for variable products, I need it to apply for all products.
Thanks in advance.
Spac-es
November 14, 2023, 3:53pm
2
Try implementing the following code:
{%- assign label_text = false -%}
{%- if settings.prod_new_show -%}
{%- if settings.prod_new_method == 'date' -%}
{%- assign now_s = 'now' | date: '%s' | plus: 0 -%}
{%- assign pub_s = product.created_at | date: '%s' | plus: 0 -%}
{%- assign diff_days = now_s | minus: pub_s | divided_by: 86400 -%}
{%- if diff_days < settings.prod_new_limit_int -%}
{%- assign label_text = 'products.labels.new_in' | t -%}
{%- assign label_css_class_suffix = 'new-in' -%}
{%- endif -%}
{%- elsif settings.prod_new_method == 'tag' -%}
{%- if product.tags contains 'New' -%}
{%- assign label_text = 'products.labels.new_in' | t -%}
{%- assign label_css_class_suffix = 'new-in' -%}
{%- endif -%}
{%- else -%}
{%- for collection in product.collections -%}
{%- assign split_collection_handle = collection.handle | split: '-' -%}
{%- if split_collection_handle contains 'new' -%}
{%- assign label_text = 'products.labels.new_in' | t -%}
{%- assign label_css_class_suffix = 'new-in' -%}
{%- break -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- if label_text == false and settings.prod_reduction_show and product.variants.first.compare_at_price > product.variants.first.price -%}
{%- if settings.prod_reduction_type == 'percent' -%}
{%- assign amount = 1.0 | times: product.variants.first.price | divided_by: product.variants.first.compare_at_price -%}
{%- assign amount = 1.0 | minus: amount -%}
{%- assign amount = amount | times: 100.0 | round -%}
{%- assign label_text = 'products.labels.percent_reduction' | t: amount: amount -%}
{%- assign label_css_class_suffix = 'sale' -%}
{%- else -%}
{%- assign amount = product.variants.first.compare_at_price | minus: product.variants.first.price | money -%}
{%- assign label_text = 'products.labels.value_reduction_html' | t: amount: amount -%}
{%- assign label_css_class_suffix = 'sale' -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- if label_text == false and product.metafields.custom.product_label != blank -%}
{%- assign label_text = product.metafields.custom.product_label | metafield_text -%}
{%- endif -%}
{%- if label_text -%}
{{ label_text }}
{%- endif -%}
Hi,
This hasn’t worked. The labels still function as normal.
If there is a value in metafield custom.product_label I would like it to ignore the percent/new/default labels and place the metafield value in the label. If the meta field is blank, then follow the default rules percent/new/default etc.
Spac-es
November 15, 2023, 2:11pm
4
This updated code first checks if there is a custom product label in the custom.product_label metafield. If it exists and is not blank, it uses that value as the label text. If the metafield is empty or doesn’t exist, it then falls back to the default label logic (percent/new/default etc) as per your previous requirements.
{%- assign label_text = product.metafields.custom.product_label | metafield_text -%}
{%- if label_text == blank -%}
{%- assign label_text = false -%}
{%- if settings.prod_new_show -%}
{%- if settings.prod_new_method == 'date' -%}
{%- assign now_s = 'now' | date: '%s' | plus: 0 -%}
{%- assign pub_s = product.created_at | date: '%s' | plus: 0 -%}
{%- assign diff_days = now_s | minus: pub_s | divided_by: 86400 -%}
{%- if diff_days < settings.prod_new_limit_int -%}
{%- assign label_text = 'products.labels.new_in' | t -%}
{%- assign label_css_class_suffix = 'new-in' -%}
{%- endif -%}
{%- elsif settings.prod_new_method == 'tag' -%}
{%- if product.tags contains 'New' -%}
{%- assign label_text = 'products.labels.new_in' | t -%}
{%- assign label_css_class_suffix = 'new-in' -%}
{%- endif -%}
{%- else -%}
{%- for collection in product.collections -%}
{%- assign split_collection_handle = collection.handle | split: '-' -%}
{%- if split_collection_handle contains 'new' -%}
{%- assign label_text = 'products.labels.new_in' | t -%}
{%- assign label_css_class_suffix = 'new-in' -%}
{%- break -%}
{%- endif -%}
{%- endfor -%}
{%- endif -%}
{%- if label_text == false and settings.prod_reduction_show and product.variants.first.compare_at_price > product.variants.first.price -%}
{%- if settings.prod_reduction_type == 'percent' -%}
{%- assign amount = 1.0 | times: product.variants.first.price | divided_by: product.variants.first.compare_at_price -%}
{%- assign amount = 1.0 | minus: amount -%}
{%- assign amount = amount | times: 100.0 | round -%}
{%- assign label_text = 'products.labels.percent_reduction' | t: amount: amount -%}
{%- assign label_css_class_suffix = 'sale' -%}
{%- else -%}
{%- assign amount = product.variants.first.compare_at_price | minus: product.variants.first.price | money -%}
{%- assign label_text = 'products.labels.value_reduction_html' | t: amount: amount -%}
{%- assign label_css_class_suffix = 'sale' -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- endif -%}
{%- if label_text -%}
{{ label_text }}
{%- endif -%}
That’s amazing! It works great. Thank you for your time.