shopoify liquid tags if else unless - shopify

I need to customize the shipping confirmation email. I want to use a tag to determine which of two text sections are included in the email. The problem is there is usually an array of tags. I can get section "A" like this...
{% for tag in tags %}
{% if tag == 'a' %}
A
{% endif %}
{% endfor %}
There is only a single 'a' tag in the array so I only get the "A" text once.
But I can't figure out how to get the "B" text to appear just one time.
If I do this, it appears for every tag that does not == 'a'...
{% for tag in tags %}
{% unless tag contains 'a' %}
B
{% endunless %}
{% endfor %}
Is there a way to get one instance of "B"?

You could repeat the same logic you did for A:
{% for tag in tags %}
{% if tag == 'a' %}
A
{% endif %}
{% if tag == 'b' %}
B
{% endif %}
{% endfor %}
Alternatively you could do a switch/case statement, I'd prefer this approach because it's easy to read, and if sometime in the future you would like to add another condition (tag), it would be easy and the code would still keep its elegance.
{% for tag in tags %}
{% case tag %}
{% when 'a' %}
A
{% when 'b' %}
B
{% when 'c' %}
C
{% endcase %}
{% endfor %}

If you have many tags it can become tricky, but if it's just two tags this is the general idea.
{% assign a_not_found = true %}
{% for tag in tags %}
{% if tag == 'a' %}
...
{% assign a_not_found = false %}
{% endif %}
{% endfor %}
{% if a_not_found %}
{... show b... }
{% endif %}
Otherwise
{% if tags contains 'a' %}
{...show a...%}
{% else %}
{...show b...%}
{% endif %}

Related

How to display current parent active category and its sub-categories only in Sidebar?

Hey I would like to display only active parent category and its available sub-categories in the category-tree in storefront/layout/sidebar/category-navigation.html.twig.
so if category A is selected then it should display all its subcategories.
example
Category A
Category A.1
Category A.2
Category A.3
This will output the tree branch to the currently active category, as well as the links to its direct descendant categories.
{% sw_extends '#Storefront/storefront/layout/sidebar/category-navigation.html.twig' %}
{% block layout_navigation_categories_list_entry %}
{% if (item.category.id in activeResult.id) or (item.category.id in activeResult.path) or (item.category.parentId in activeResult.id) %}
{{ parent() }}
{% endif %}
{% endblock %}
Update: If you want to hide the parents up to the currently viewed category, this should do the trick.
{% block layout_navigation_categories_list_entry %}
{% if item.category.id in activeResult.path %}
{% block layout_navigation_categories_recoursion %}
{% if item.category.id in activeResult.id %}
{% set levelIncrement = 1 %}
{% else %}
{% set levelIncrement = 0 %}
{% endif %}
{% sw_include '#Storefront/storefront/layout/sidebar/category-navigation.html.twig' with {
navigationTree: item.children,
activeResult: activeResult,
level: level + levelIncrement
} only %}
{% endblock %}
{% endif %}
{% if (item.category.id in activeResult.id) or (item.category.parentId in activeResult.id) %}
{{ parent() }}
{% endif %}
{% endblock %}

can i use in_array () in Shopify

I am new To Shopify ! i am trying to something like,i want to check that,the ID which i put on Textbox is in array or not !
here is my Product.Liquid
{% if settings.make_an_offer %}
{% for id in product.id %}
{% include 'pricewaiter' %}
{% endfor %}
{% endif %}
in above code,i need to check that settings.make_an_offer is in this array product.id or not..
make_an_offer this is an ID of my Textbox
so how can i do this?
any help please?
Place it like this :
{% for id in product.id %}
{% if settings.make_an_offer === id %}
{% include 'pricewaiter' %}
{% endif %}
{% endfor %}

Output Shopify Article by Article ID

The loop for Shopify Blog Articles is:
{% for article in blog.articles %}
{% endfor %}
I'm wondering if it's possible to output articles by different ID. Let's say 33, 65, 81. I have this custom field where I can write the ID's I want and is outputted by:
{% if article.metafields.c_f.recommended_post %}{% else %}
{% endif %}
Any ideas?
If the metafield contains a single article ID and is saved in Shopfiy as an integer, you can basically just combine the two pieces of code you already have:
{% for article in blog.articles %}
{% if article.id == article.metafields.c_f.recommended_post %}
{% comment %} Found recommended article {% endcomment %}
{% else %}
{% endif %}
{% endfor %}
If your metafield contains a single article ID but is saved in Shopify as a string you need one extra step to covert the metafield value to an integer:
{% for article in blog.articles %}
{% assign recommended_post = article.metafields.c_f.recommended_post | plus: 0 %}
{% if article.id == recommended_post %}
{% comment %} Found recommended article {% endcomment %}
{% else %}
{% endif %}
{% endfor %}
If your metafield contains multiple article IDs, separated by commas like "33, 65, 81" you can use the split filter and contains operator, and convert your article id to a string instead:
{% assign recommended_ids = article.metafields.c_f.recommended_post | split: ', ' %}
{% for article in blog.articles %}
{% assign article_id = article.id | append: '' %}
{% if recommended_ids contains article_id %}
{% comment %} Found one of many recommended articles {% endcomment %}
{% else %}
{% endif %}
{% endfor %}
For more information on saving metafields as either strings or integers see: https://docs.shopify.com/api/metafield

How to hide products in Shopify search results based on a vendor name

I'm in a situation where hiding a certain vendors products in the control panel isn't an options due to an outside POS. For a test in search.liquid, I used search.terms like below. This code works but not everyone will type thevendor exactly the same way and will see the products if they don't type thevendor.
{% for item in search.results %}
{% if search.terms == 'thevendor' %}
{% else %}
{% include 'search-result' %}
{% endif %}
{% endfor %}
I tried to figure out how to write the code to hide these products in a better way. I tried product.vendor like below but when I search for those products individually they are not hidden. The code:
{% for item in search.results %}
{% if product.vendor == 'thevendor' %}
{% else %}
{% include 'search-result' %}
{% endif %}
{% endfor %}
Can someone tell me what I'm missing here? It seems it doesn't know what product.vendor is but when I print out who the vendor is, it displays the vendor. I don't understand why it's not hiding the products that are associated with this vendor.
{% for item in search.results %}
{% if item.product.vendor == 'thevendor' %}
{% else %}
{% include 'search-result' %}
{% endif %}
{% endfor %}
This should work.

How can I check for more than 1 product with same name?

I need to check if there are two or more products with the same name so I can add an if/else statement. So I'm thinking along the lines of:
{% if product.title == product.title %}
or
{% if product.title.size > 1 %}
But this doesn't seem to be right.
Thanks!
Are you looking for a specific product title, or any product title that is duplicated? If you are comparing against a given product title, perhaps try something like this:
{% assign found_duplicate = false %}
{% for p in products %}
{% if p.title == product.title %}
{% assign found_duplicate = true %}
{% endif %}
{% endfor %}
{% if found_duplicate == true %}
...
{% endif %}