Was first looking for a means to select certain existing products in order to place title, image, description on another page (or even within another product page - as some products are combined to make other products) within Shopify.
The method below was the only one I seem to come across for creating an array within Shopify. (the split method).
The next part of the equation, is to use the values from {{ myArray }}, to select the matching var, and to then spit out the different values stored within that array.
However, my attempt does not work. Is there a way to add keys to the other arrays (i.e, p1, p2, p3 arrays), in order to make the selecting of them easier during the for loop?
{% assign myArray = "p1|p3" | split: "|" %}
{% assign p1 = "Product One|product-one|This is my description of product one, and it must be a single paragraphy without any html formatting. The length is not an issue.|product_one_image.jpg" | split:"|" %}
{% assign p2 = "Product Two|product-two|This is my description of product two, and it must be a single paragraphy without any html formatting.|product_two_image.jpg" | split:"|" %}
{% assign p3 = "Product Three|product-three|This is my description of product three, and it must be a single paragraphy without any html formatting.|product_three_image.jpg" | split:"|" %}
{% for item in myArray %}
<h4>{{ item[0] }}</h4>
<p>{{ item[2] }}</p>
{% endfor %}
Continuity flow is wrong in your code.
Here's what you need to do
Load product elements for each product into an array
{% capture list %}
{% for product in products %}
{{ product.title }}|{{ product.handle }}|{{ product.description}}|{{ product.featured_image }}{% if forloop.last %}^{% endif %}
{% endfor %}
{% endcapture %}
{% assign p_list = list | split: "^" %}
Now p_list contains all the products as each element in the array. It is time to get the output.
{% for p_item in p_list %}
{% assign item = p_item | split: "|" %}
<h4>{{ item[0] }}</h4>
<p>{{ item[2] }}<p>
{% endfor %}
Once you start the for loop it will iterate through each value in your array, so indexing (using []) won't work since the array is already being iterated.
In the example above, you began iterating through the list and then attempted to index item, which will not work. If you wanted to index the array then do not make a for loop, in the list above the array only has 2 items but you selected an index outside of the available indexes, this is because the array position starts at 0. so that <p> tag should've been item[1] and outside of the for loop.
To do a for loop. do this:
{% for item in array %}
<h4>{{ item }}</h4>
{{ continue }}
<p>{{ item }}</p>
{% endfor %}
The continue tag will cause it to iterate to the next item on the for loop.
Related
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 %}
Is there a way to get all the products starting with title A or B or C in Shopify without using collection.
Unfortunately this might be not possible.
Ideally this would work but it doesn't:
{% assign my_products = collections['all'].products | where: "title.first", "A" %}
{% for product in my_products %}
{{ product.title }}
{% endfor %}
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
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 need to display n number of images for a product in shopify.
I have stored number of images in a metafields and created a loop for it.
Then each image's name is stored in a metafield, which i am trying to get with help of loop.
{% assign earrings = product.metafields.earrings %}
{% for i in (1..earrings.total-earrings) %}
{% assign earring = 'product.metafields.earring-' | append:i %}
{{ earring.name }}
{% endfor %}
This loop is giving me values for earring like:
product.metafields.earring-1
product.metafields.earring-2
but when i am trying to read value of metafield earring.name, i am not getting any output. I think because product.metafields.earring-1 is a string.
Is there any possible way to loop through metafields like this and get values?
Just in case it's helpful for someone.
Here's the updated code:
{% assign earrings = product.metafields.earrings %}
{% for i in (1..earrings.total-earrings) %}
{% assign dummy = 'earring-' | append:i %}
{% assign earring = product.metafields[dummy] %}
{{ earring.name }}
{% endfor %}