Liquid - Convert Array to Lowercase - shopify

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.

Related

In shopify How to push product Object to blank Array

In Shopify my code structure follows product loop.
{% assign products = all_products[block.settings.product_to_show] %}
In products variable i got object of one product.
but my code structure of for loop only accept products as array.
{% for product in products %}
{% include 'product-card', product: product %}
{% endfor %}
So how can i push "products" (object) in blank array in shopify?
With Liquid we are generally limited to creating an array of strings (not objects). Given that you seem to have the product handles coming from section block settings, here are some approaches that may work for you:
Use a forloop on section.blocks, create the product object and pass it to the output snippet (Example code assumes there is only 1 product per block).
{% for block in section.blocks %}
{% assign _product = all_products[block.settings.product_to_show] %}
{% include 'product-card', product: _product %}
{% endfor %}
Loop over the section blocks and create a comma separated string of the product handles(strings). Use the split filter to convert the string into and array of strings. Loop over the array, create the product object and pass it to the output snippet.
{% assign products = '' %}
{% for block in section.blocks %}
{% comment %}
You can place additional logic/conditions within this loop to customize how your "products" array is built
{% endcomment %}
{% assign products = products | append: block.settings.product_to_show | append: ',' %}
{% endfor %}
{% assign products = products | split: ',' %}
{% for product_handle in products %}
{% assign _product = all_products[product_handle] %}
{% include 'product-card', product: _product %}
{% endfor %}

Passing previously assigned variable in `{% for` block in Shopify

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.

working with 'strings' as 'arrays' in shopify liquid

I am trying to display color boxes next to items based on item varients. However, it has been giving me weird results in my array. Yes I know there are no real arrays in liquid. I have two options below. The first one doesnt work. It gives me things like "background-color: [''''''' ". Along with all the correct ones too.
So the second option i just hard coded all the colors and checked against that. This works as long as the colors are in order... but if the colors are not in order than it will display duplicates.
New to liquid but this seems super ugly and probably means i am doing it wrong.
<div class="color-box-wrapper">
{% assign values = '' %}
{% for variant in product.variants %}
{% assign value = variant.option2%}
{% unless values contains value %}
{% assign values = values | append: ',' | append: value %}
{% assign values = values | split: ',' %}
{% for color in values %}
{% if color.size > 0%}
<div class="product-color-box" style="background-color:{{color}}"></div>
{% endif %}
{% endfor %}
{% endunless %}
{% endfor %}
</div>
THIS WAY KINDA WORKS BUT SEEMS HACKY.
<div class="color-box-wrapper">
{% assign realColors = 'yellow, blue, white, burgandy, black, red, green, purple, beige, light_brown' | split: ", "%}
{% assign values = '' %}
{% for variant in product.variants %}
{% assign value = variant.option2 | downcase%}
{% unless values contains value %}
{% assign values = values | append: ',' | append: value %}
{% assign values = values | split: ',' %}
{% for color in values %}
{% if realColors contains color %}
<div class="product-color-box" style="background-color:{{color}}"></div>
{% endif %}
{% endfor %}
{% endunless %}
{% endfor %}
</div>
It might work better to use the product.options_with_values field, something like this:
{% assign color_option = product.options_with_values | where: 'name', 'color' | first %}
<h1>Color option is in position {{ color_option.position }}!</h1>
<h2>Array of all values is: {{ color_option.values | json }}</h2>
{% for value in color_option %}
<h3>Gimmie a {{ value }}!! {% if value == color_option.selected_value %}(Selected){% endif %}</h3>
{% endfor %}
It's a bit trickier if your colours are not CSS-recognized colour names, but there are certainly a number of things you can do for that. I typically prefer adding a CSS layer that can translate colour values into the appropriate display values (either background images or colour hex-codes). Some ideas are:
Add a data attribute or a class to your element (usually using the | handle filter to standardize the output) and use a CSS sheet to assign background images or colours appropriately
Create a section with blocks that allows you to map colour values to hex codes. If you are creating this for someone other than yourself, it would allow the merchant to set up the colours themselves and fine-tune all the shades.
Use metafields on your product that can generate the correct colour code using the options as the lookup. (Eg: If you create a metafield namespace on your products of product.metafields.colors and use the colour names as the keys and hex codes as the values, you can output {{ product.metafields.colors[value] }} to get the right computer colour. (This generally requires installing an app to manage - though metafields themselves are native Shopify functionality, Shopify doesn't have any native way to set them in the admin)
Hope this helps!
References:
Shopify Liquid Reference - Product objects: https://help.shopify.com/en/themes/liquid/objects/product#product-options_with_values
Shopify Liquid Reference - Product Option objects (from options_with_values): https://help.shopify.com/en/themes/liquid/objects/product_option

How to use a variable as a key in a hash in liquid (Shopify)

I would like to set up a hash variable using strings from an array dynamically (instead of writing 1000 lines of code).
As well I would like to use a dynamically created string to access a hash by using it as the key - for a built-in (what I assume is a hash) object - settings. Settings allow you to access data in settings_schema.json, eg: settings.my_custom_setting
According to this documentation: https://github.com/Shopify/liquid/wiki/Liquid-for-Designers
"For hashes, the key must be a literal quoted string or an expression that resolves to a string."
so I have tried {% assign filter[thisFilter] = false %} but get an error: ParseError: illegal token
First issue / accessing hash key with a variable:
{% comment %} All possible filters {% endcomment %}
{% assign allFilters = "color,size,collection,style,height,function,artist" %}
{% assign allFiltersArray = allFilters | split ',' %}
{% comment %} hash of filters each set to false {% endcomment %}
{% for thisFilter in allFiltersArray %}
{% assign filter[thisFilter] = false %}
{% endfor %}
Second issue, accessing settings object with a dynamically generated key:
{% comment %} set to true whichever filters are configured in settings for this collection {% endcomment %}
{% for thisCollection in allCollectionsArray %}
{% if thisCollection == currentCollection %}
{% for thisFilter in allFiltersArray %}
{% assign itemToCheck = "filter_" | append: thisCollection | append: "_" | append: thisFilter %}
{% if settings[itemToCheck] %}
{% assign filter[thisFilter] = true %}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
In the first issue, I expect the result to be a hash such as:
filter['color'] = false (or filter.color = false)?
filter['size'] = false
In the second issue, I'm expecting something like:
{% if settings.filter_shirts_color %}
What you are trying to do is not possible. If you read further on your provided link Liquid for Designers, it is mentioned
Note that there is no way to write a literal array or hash as an
expression; arrays and hashes must be passed into the template, or
constructed obliquely with a tag or output statement.
Moreover, even if you have such hash, you cannot assign it new value. For example,
{% assign settings['some-setting-id'] = false %}
This will not work. Same is the case with array created using split filter. You cannot assign new values on any index.
For the second issue, this should work, the error in your case most probably is because of invalid generated string or there is no setting with that id. This should work fine and display value for that setting.
{%assign string_key = 'setting-key'%}
{{settings[string_key]}}
But this will not work because
{%assign string_key = 'setting-key'%}
{{settings.string_key}}
my_hash.key — Hashes also allow a shorter "dot" notation, where the
name of the variable is followed by a period and the name of a key.
This only works with keys that don't contain spaces, and (unlike the
square bracket notation) does not allow the use of a key name stored
in a variable.

can i use OR in Shopify case

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' %}