specify variable array index for liquid - shopify

My liquid code is
{% assign _product_id = product.id %}
{% assign _product_tag = product.collections[0].title}
{% assign _product_name = product.title %}
{{_product_tag}}
{% assign pagla_array = collections[_product_tag].products %}
{{ pagla_array.first.title }}
Here last line showing nothing. if I use a static index for assigning pagla_array like {% assign pagla_array = collections['Beans'].products %} then it show value. What wrong did I make here?

This line:
{% assign _product_tag = product.collections[0].title}
Is not closed correctly. It should end with %}
In addition you should use handles for the collections, not title.
So it should become:
{% assign _product_tag = product.collections[0].handle %}
....
{% assign pagla_array = collections[_product_tag].products %}

Related

On Shopify, check if a given product is not on any collection

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

How to use variables in Twig filter 'replace'

Handing over an array from php of form
$repl_arr = array('serach-string1' => 'replace1', ...)
to a Twig template I would like to replace strings in a Twig variable per replace filter similar to this:
{{ block | replace({ repl_arr }) }}
That does not function and neither a variable loop like
{% for key,item in repla_arr %}
{% set var = block | replace({ key : item }) %}
{% endfor %}
does. What is wrong with it? How could it work?
Either you pass the whole array, or you loop the replaces.
But when looping the replaces you need to wrap key and value in parentheses to force interpolation of those
{% set replaces = {
'{site}' : '{stackoverflow}',
'{date}' : "NOW"|date('d-m-Y'),
} %}
{% set haystack = '{site} foobar {site} {date} bar' %}
{{ haystack | replace(replaces) }}
{% set output = haystack %}
{% for key, value in replaces %}
{% set output = output|replace({(key) : (value),}) %}
{% endfor %}
{{ output }}
fiddle

Shopify - Increment or Counter

I'm trying to set up a simple way to increment within for loops in my themes and can't figure out how to get it to work. I'm familiar with two ways to increment:
{% assign variable = 0 %}
{% for .....
{% assign variable = variable | plus: 1 %}
.... endfor %}
and
{% assign variable = 0 %}
{% increment variable %}
However neither of these work. Update: Currently the following block of code will output "0" when it should be "1"
{% assign variable = 0 %}
{% assign variable = variable | plus: 1 %}
{{ variable }}
What am I doing wrong?
What you are doing with the assign should work however there is an easier way:
{{forloop.index0}}
See the docs for the loop object

Looping through metafields in shopify

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