Is it possible to add more values in a for in loop Liquid Shopify? - shopify

Here's my code
{% for filter_prefix in filter_prefixes %}
{{ filter_prefix }}
{% endfor %}
There are some values I want to add to the filter_prefixes, example: Type...
Can I do it like this?
{% for filter_prefix in filter_prefixes %}
{% filter_prefix = 'Type' %}
{{ filter_prefix }}
{% endfor %}
To list all the filter_prefixes and added the Type value as well.
Please help me with this.
Thank you!

Just output the value you want. There is no need to add it to the loop since it is just something you output anyway.
Type
{% for filter_prefix in filter_prefixes %}
{{ filter_prefix }}
{% endfor %}
Or use the Shopify Array filters split and join. So you join your existing array filter_prefixes into a string, append Type and then split that back into an array.

Related

Use a Twig variable as a part of an other

I would like to use a Twig value comes from a "for" loop as a key in another for loop.
I have 2 arrays: "tab" dans "stats".
"tab" array has multiple values, with one named "name", which is an other array with keys and values.
I would like to use the "tab.name" value as a loop variable for my stats array.
I tried with no success to "add" the value directly in my variable.
{% for elem in tab %}
{% for data in stats.elem.name %}
------ My code
{% endfor %}
{% endfor %}
With this I don't have any result because Twig search for "stats.elem.name" array, but in fact for example if in a loop elem.name = "intitule", the real array is stats.intitule.
I hope i'm clear enough… :)
Thanks!
this should work
{% for elem in tab %}
{% for data in stats[elem] %}
{{data}}
{% endfor %}
{% endfor %}
https://twigfiddle.com/mzpjh8
You can do this by changing the array reference for stats to the standard square brackets - stats[elem.name]
{% for elem in tab %}
{% for data in stats[elem.name] %}
<!-- code -->
{% endfor %}
{% endfor %}

Django comparing form field value with selected value

I have a number of select box form fields which I need to output in the template with custom markup as a list. To do this I have created an include which gets passed the current form field as an argument.
I can loop over the list of options to create the list I want without any problems. My problem is that above the list of options I need to display either the form label, or the current selected value. This is the code I have to do this:
{% if field.value %}
{% for value, option in field.field.choices %}
{% if value == field.value %}
{{ option }}
{% endif %}
{% endfor %}
{% else %}
{{ field.label }}
{% endif %}
If for example value in the above code is 1, and field.value is also 1, the equality test doesn't work and the bit of code within the if statement does not get executed. I guessed it might be down to the data type of the two values I was comparing, so I tried casting both to an integer as follows:
{% if field.value %}
{% for value, option in field.field.choices %}
{% if value|add:"0" == field.value|add:"0" %}
{{ option }}
{% endif %}
{% endfor %}
{% else %}
{{ field.label }}
{% endif %}
This worked fine for the case where the values in the select box were integers. However, sometimes my select boxes have booleans as the value, so in this case the comparison won't work with the integer casting applied.
How can I safely compare the two values whatever the type of data is being compared?
I think I have found the answer.
The issue is that the selected value is always a string, and the value in the forloop could be an integer or a boolean (or potentially other data types). The answer is to cast the value in the forloop to a string:
{% if field.value %}
{% for value, option in field.field.choices %}
{% if value|stringformat:"s" == field.value %}
{{ option }}
{% endif %}
{% endfor %}
{% else %}
{{ field.label }}
{% endif %}

How to append a variable inside another vaiable name in liquid html

Here is the code:
{% for i in (0..5) %}
{% assign product = recommendations.top_related_products_{{i}}.products[0].title %}
{{ product }}
{% endfor %}
This code throws the error Liquid error: Cannot read property '0' of null
I have 6 arrays under recommendation like top_related_products_0...5.
How do I print the title variable which is inside the top_related_products_0..5?
Create a string containing the variable name, then use square bracket notation to access it.
For example:
{% for i in (0..5) %}
{% capture related_products %}top_related_products_{{i}}{% endcapture %}
{{ recommendations[related_products].products.first.title }}
{% endfor %}

shopify pass a variable to settings

i want to do something like this in Shopify:
{% for i in (0..10) %}
{% capture slide %}slide{{i}}{% endcapture %}
{{ settings.slide }}//i need the value of this one
// i want to get the values for settings.slide1, settings.slide2 etc
{% endfor %}
Another example:
{% for i in (0..10) %}
{{ settings.slide[i] }}//i need the value of this one
{% endfor %}
This is a simplified version of what im trying to achieve.
Thanks
Try this:
{% for i in (0..10) %}
{% assign current_slide = 'slide' | append: i %}
{{ settings[current_slide] }}
{% endfor %}

Shopify If in collection then display this

I am trying to write a simple if statement, but always struggle with shopify's system.
Essentially I want it to do this:
{% if collection.product == 'discontinued' %}
This Product is Discontinued.
{% endif %}
If it's in this collection, then display this text/html. Otherwise it wouldn't display anything. This would be in the product.liquid template.
Any ideas?
This is what ended up working:
{% for c in product.collections %}
{% if c.handle == "discontinued" %}
This product is Discontinued
{% endif %}
{% endfor %}
You can create an array of the collections for a product using map on product.collections. This which will create a new array with your specified property, i.e. the handles of each collection.
You can then check if this new array contains the handle you want to work with.
{% assign productCollections = product.collections | map: "handle" %}
{% if productCollections contains 'your-collection-handle' %}
{% comment %} DoSomething {% endcomment %}
{% endif %}
So for your example:
{% assign productCollections = product.collections | map: "handle" %}
{% if productCollections contains 'discontinued' %}
This product is Discontinued
{% endif %}
You can map other fields if your case is different, such as the title.
I guess this will help any one, I have used in the sidebar of shopify website.
The current collection page will get checked by this below code.
<div class="row-fluid not-animated" data-animate="fadeInUp">
<div class="title">By Collections</div>
<form class="coll">
{% assign col_tags = collection.title %}
{% for collection in collections %}
<input type="radio" value="{{ collection.url }}" name="collections" {% if col_tags contains collection.title %} checked {% endif %} >{{ collection.title | escape }} <br/>
{% endfor %}
</form>
If I understand how liquid collections work in Shopify, you will need to iterate over all of your products.
You'd need to do something similar to this if you are working with collections directly:
{% for product in collection.product %}
{% if product.tags contains 'discontinued' %}
This product has been discontinued :(
{% endif %}
{% endfor %}
If you are just working with a single product you can probably just use the inner if liquid tag part.
References:
Collection.liquid
Product.liquid
You can indeed add discontinued products to a collection called discontinued.
When rendering a product, you could do as csaunders suggests, simply loop through all the products in the discontinued collection, and check if the id of the current product matches any of the products in that collection. If so, do what you must do. No need to use tags.