let say I have three products and made a case
I want to know if I can use OR instead of duplicating the content again
{% case shipping_method.title %}
{% when 'packageA' || 'packageB' %}
around 5000 lines
{% when 'packageC' %}
{% endcase %}
Yes you can but it must be a valid syntax. || is not a valid operator in liquid.
You have to use or instead so it becomes {% when 'packageA' or 'packageB' %}
Related
Within the product page, I would like to hide a certain element if the current product isn't associated with any collection. Is there any way to check it with Liquid ?
The object product.collections is an array containing all collections where the product is listed.
So you may use this:
{% if product.collections.size < 1 %}
Do something
{% endif %}
Documentation about arrays here:
https://shopify.dev/api/liquid/filters#size
{% if 0 == product.collections | size %}
!! put your code here !!
{% endif %}
Or
{% for _ in product.collections %}
{% else %}
!! put your code here !!
{% endif %}
In blog-templte.liquid
{% assign articleSortOrder = '' %}
....
{% for article in blog.articles {{articleSortOrder}} %}
got an error : Liquid syntax error: Unexpected character { in "article in blog.articles {{articleSortOrder}}"
The intention is to pass the variable to sort the articles depending on some condition.
Q: is how to make it work?
This is not a valid liquid code:
{% for article in blog.articles {{articleSortOrder}} %}
You can't pass a liquid inside a liquid, a.k.a {% {{ }} %}
In addition for loops accept only a few parameters:
reversed - which will reverse the loop
limit - which will limit the iterations
offset - which will make the loop skip a specific set number of items
Sort is not one of them.
You can read more about the for loop here: https://shopify.dev/docs/liquid/reference/tags/iteration-tags
In order to sort the blog in a specific way you must code it like so:
{% assign articleSortOrder = '' %}
{% assign blog_articles_sort = blog.articles | sort: articleSortOrder %}
{% for article in blog_articles_sort %}
{% endfor %}
Where you assign the articles in a specific variable and sort them.
Please have in mind that this will sort ONLY 50 articles.
If you like to sort more than 50 you will need to overwrite the paginate object {% paginate blog.articles by 9999 %}
Then your code will look like this:
{% paginate blog.articles by 9999 %}
{% assign articleSortOrder = '' %}
{% assign blog_articles_sort = blog.articles | sort: articleSortOrder %}
{% for article in blog_articles_sort %}
{% endfor %}
{% endpaginate %}
More about paginate can be seen here: https://shopify.dev/docs/liquid/reference/tags/theme-tags/#paginate
Please have in mind that the sort function in Shopify is limited. You may need to sort them with javascript or another approach depending one what you are looking for.
I'm using Shopify and want to hook into customer tags, however they are case sensitive. So {% if customer.tags contains "wholesale" %} is not the same as {% if customer.tags contains "Wholesale" %}. My client may or may not stick to one case when applying tags so I want to guard against that in the future.
I would like to take an array, customer.tags, and convert all of the values to lowercase. I'm trying to work out the logic but am having trouble.
I want to put customer.tags into a new array which isn't working.
{% assign newArray = customer.tags %}
{{ newArray }}
What am I doing wrong?
You could use the downcase filter for this:
{% assign contains_wholesale = false %}
{% for tag in customer.tags %}
{% assign lowercase_tag = tag | downcase %}
{% if lowercase_tag == 'wholesale' %}
{% assign contains_wholesale = true %}
{% endif %}
{% endfor %}
Note: downcase just works for ASCII characters. If you need to search for strings with accented letters or other Unicode characters then this won't be enough.
if you would like to keep customer.tags as an array so you can keep using contains in a simple if statement (like your example). You could also chain a couple of liquid filters together to turn all of the strings within the array to lowercase.
Example:
{% assign lowercaseTags = customer.tags | join: ',' | downcase | split: ',' %}
{% assign randomString = 'WholeSale' | downcase %}
{% if lowerCaseTags contains randomString %}
{% comment %}
Will now match regardless of case sensitivity
{% endcomment %}
{% endif %
Explanation:
Join filter: Turn the array into a string seperated by ,
Downcase filter: make the whole string lowercase
Split filter: opposite of join, recreates the array from the string based on the character used in join ,
Another solution as you use the "contains" operator would be to skip the "w".
Something like {% if customer.tags contains 'holesale' %} should work.
I'm trying to iterate through a Model collection in volt:
{% if model.elements|length > 0 %}
{% for element in model.getElements() %}
{% include "partials/panels/edit-" ~ element.getType() ~ ".volt" %}
{% endfor %}
{% endif %}
The type can be text or images. If i use the above code, i get the error:
View '/path/to/phalcon/apps/frontend/views/partials/panels/edit-image.volt' was not found in the views directory
I'm sure that the file exists, since if i changethe include, it'll work:
{% include "partials/panels/edit-image.volt" %}
It'll also fail on:
{% include "partials/pandels/edit-" ~ "image.volt %}
What is the reason that the first version is producing that error?
( I know i could just use ifs.. But theres quite a list of element types later on. )
This will not work.
To include view dynamically use partial:
{% if model.elements|length > 0 %}
{% for element in model.getElements() %}
{{ partial( "partials/panels/edit-" ~ element.getType() ) }}
{% endfor %}
{% endif %}
There is no '.volt' since partial will add it.
Good evening! I am trying to remove variants from the variant array on a product using pure Liquid(Shopify templating language). I would only like to use javascript as a last resort.
Below is where I am so far. Anything that is the variant in the if check needs to be removed from currentProduct.variants.
{% assign currentProduct = product %}
{% for variant in currentProduct.variants %}
{% include 'shappify-variant-is-csp' %}
{% if csp_variant != 1 %}
//need to remove the object that meets this if statement
{% endif %}
{% endfor %}
I'm pretty sure you're going to need to use some javascript to achieve this. Take a look at this article on the Shopify wiki: How do I remove sold out variants from my options drop-downs.
Modifying the code in that article for your situation, you'll want something like this:
{% for variant in product.variants %}
{% include 'shappify-variant-is-csp' %}
{% if csp_variant != 1 %}
jQuery('.single-option-selector option').filter(function() { return jQuery(this).html() === {{ variant.title | json }}; }).remove();
{% endif %}
{% endfor %}
jQuery('.single-option-selector').trigger('change');