Hey all, I am trying to display a message depending on product tags and variant inventory management. If a product is tagged ‘2 Weeks’ it will display a certain message, same thing for when a product is tagged ‘4-6 Weeks’. The last condition is when the variant inventory management is set to null it will display another message. The issue comes when some of the products that are tagged ‘2 Weeks’ have variant inventory management set to null, it is displaying the VIM message instead of the 2 week message. Here is the code:
Do you have any other tags on the product than 2 Weeks or 4-6 Weeks? If so, that could be the issue, as you are looping over all tags.
There are quite a few more product tags. That makes sense now that I think about it more. Is there a solution to bypass this?
You could either use metafields instead of tags or just add the tag 2-3 Weeks, when you need it and change your code:
{% for tag in product.tags %}
{% if tag == '2 Weeks' %}
{%- assign option_text = 'Estimated Production Time is 2 weeks' -%}
{% elsif tag == '4-6 Weeks' %}
{%- assign option_text = 'Estimated Production Time is 4-6 weeks' -%}
{% elsif tag == '2-3 Weeks' and variant.inventory_management == null %}
{% assign option_text = 'Estimated Production Time is 2-3 weeks' %}
{% endif %}
{% endfor %}
The even better solution would be to not iterate over the array, but to check for the values:
{% if product.tags contains '2 Weeks' %}
{%- assign option_text = 'Estimated Production Time is 2 weeks' -%}
{% elsif product.tags contains '4-6 Weeks' %}
{%- assign option_text = 'Estimated Production Time is 4-6 weeks' -%}
{% elsif variant.inventory_management == null %}
{% assign option_text = 'Estimated Production Time is 2-3 weeks' %}
{% endif %}
1 Like
This works perfectly! thank you!
1 Like