Making a custom collection in shopify liquid - liquid - ruby

I want to make a custom collection in collection.liquid based on some conditional scenario and for this I applied concat, append and join but the problem is that it returns ProductDropProductDropProductDropProductDrop... or LazyProductDropCollectionLazyProductDropCollection instead of products. Following is the code snippet
{% assign custom_products = '' %}
{% for product in collections["paneer-easy-indie-bowls"].products %}
{% assign custom_products = custom_products | append: product %}
{% endfor %}
instead of append I tried both join and concat but returns ProductDropProductDropProductDropProductDrop...
{% assign custom_products = custom_products | concat: product %}
then I tried the following:
{% capture custom_products %}
{% for product in collections["paneer-easy-indie-bowls"].products %}
{{ custom_products }},{{ product.handle }}
{% endfor %}
{% endcapture %}
{% assign custom_products = custom_products | split: ',' %}
{% for product in custom_products %}
{{ product}}
{% endfor %}
but this code not appending the products in right way. I want products like same as{{collection.products}}. Any suggestion ?

If I read your pseudo-code correctly, you are trying to build a collection of products from a collection of products. Which leads to the question, why? Since you already have a perfect collection, use it as is!

Related

How to show name specific tag in shopify

I want to display the value of a specific product tag in my shopify store.
The product tag always starts with Color_ followed by the color Color_Green
I want to display only green. (I can use split or slice for that part)
But I can't figure out to only display the tag that starts with Color_ because I have multiple tags added to my products.
This is my code for now:
{%- for tag in product.tags -%} {%- assign tag_prefix = tag %} {%- if tag_prefix == "Color_" -%} {%- endif -%} {%- endfor -%}
Try this snippet
{% for tag in product.tags %}
{% assign tag_prefix = tag %}
{% if tag_prefix contains "Color_" %}
{% assign tagValue = tag_prefix | replace: "Color_", "" %}
{{ tagValue }}
{% endif %}
{% endfor %}

Create a filter list of items where a filter item appears only if there is a story for the product

I'm trying to figure out how to create a filter list where it grabs the product title from a _products directory and it will show the title in the list only if there is a story with the title of the product.
I have a _products list with 11 items and I can show them all, but I want to remove an item from the list of filter items if it's title != a element in the front matter of a list of stories in a separate directory.
{% assign product = site.products %}
{% for product in site.products %}
{% for success in site.success-stories %}
{% if product.title == success.product %}
<li><p class="filter label label-default" data-filter=".{{product.title | downcase}}">{{product.title}}</p></li>
{% endif %}
{% endfor %}
{% endfor %}
What this does is shows only the items that have a matching success-story but if there are multiple stories with the same product in it's front matter it will show that product more than once.
I've got the filtering down, it's just showing the items I can't get down.
Thanks!
I have a feeling you are looking for the contains filter.
Taken directly from the Liquid for Designers wiki:
# array = 1,2,3
{% if array contains 2 %}
array includes 2
{% endif %}
In your case, I'm guessing you want:
{% assign product = site.products %}
{% assign successfulProducts = site.success-stories | map: 'product' %}
{% for product in site.products %}
{% if successfulProducts contains product.title %}
... {{ product }} ...
{% endif %}
{% endfor %}
All I've done here in line 2 is mapping success-stories to successfulProducts. Essentially each story element is replaced by its story.product property.
Then I loop through all the products like you and check each one if it should be displayed. But I don't loop through all the stories - instead I put a simple condition that checks if successfulProducts contains the product title, just as you specified.

How do I show a tag to represent multiple products? Shopify Liquid

Hello and thanks for reading my post!
I have a collection with multiple products. On a custom collection template, I want to show the tags only for those that contain multiple products (or when more than 1 product in that collection have the same tag)
I assume it would go something like:
{% for tag in collection.all_tags %}
{% if tag.product.size >= 1 %}
has more than 1 product.
{% endif %}
{% endfor %}
I've answered similar questions here and here.
You want something like this:
{% for tag in collection.all_tags %}
{% assign products_count = 0 %}
{% for product in collection.products %}
{% if product.tags contains tag %}
{% assign products_count = products_count | plus: 1 %}
{% endif %}
{% endfor %}
{% if products_count > 1 %}
{{ tag }}
{% endif %}
{% endfor %}

Capturing Item Index in Collection

I was wondering what is the proper way of finding the index of an item in an array is in a Liquid template, and selected related items based off of the index. Currently I'm able to calculate the value, but it seems to be a string and I can't then find other items in the array with the string. For example in a CMS:
{% for site_page in site.pages.all %}
{% if site_page.id == page.id %}
{% assign page_index = forloop.index0 %}
{% capture previous_page_index %}
{{ page_index | minus: 1 }}
{% endcapture %}
{% break %}
{% endif %}
{% endfor %}
The expected value can be found in previous_page_index (in this case 0) however, if i try to do something like site.pages.all[previous_page_index] I receive no output. If I do the same thing with a hardcoded index value: site.pages.all[0] it does yield an output. Does anyone have an idea/example on how this is supposed to be done in liquid?
Best I can figure is to use {% for item in array limit:1 offset:forloop.index0 %}. For example:
require 'liquid'
chars = %w[a b c]
names = %w[alpha bravo charlie]
puts Liquid::Template.parse(<<DONE).render( 'chars'=>chars, 'names'=>names )
{% for c in chars %}
{{c}} is
{% for n in names limit:1 offset:forloop.index0 %}{{n}}{% endfor %}
{% endfor %}
DONE
…which produces…
a is
alpha
b is
bravo
c is
charlie
Editorial aside: ouch. What an ugly tempting language. I understand its goals, but the burden it places on users is heinous.

Related Products Shopify Liquid

I'm trying to determine the correct Shopify Liquid syntax for outputting a list of products that match the same tag as the current product.
This is to appear in a "Related Products" box on the product page, and I'd like it only to list other products that match the same tag of the current product page.
Unfortunately the Related Products wiki page didn't help me with this.
I'm not sure you can get a set of all products with a common tag (although I may be wrong) but here's a possible alternative way to approach it - create a smart collection of products that contain that tag, then output the products from that collection in the related items area.
To connect the product tag to the right collection on the product page, make sure that your collection handle is the same as the tag you're using, then do something like this to grab the right collection based on the tag.
{% for c in collections %}
{% assign t = {{product.tags[0] | handleize}} %}
{% if c.handle == t %}
{% assign collection = c %}
{% endif %}
{% endfor %}
Then just output the products in the collection using the approach outlined in the wiki article you linked.
Something like this (assuming you use a "product-loop" include approach) should do the trick:
{% assign current_product = product %}
{% assign current_product_found = false %}
{% for product in collection.products %}
{% if product.handle == current_product.handle %}
{% assign current_product_found = true %}
{% else %}
{% unless current_product_found == false and forloop.last %}
{% include 'product-loop' with collection.handle %}
{% endunless %}
{% endif %}
{% endfor %}

Resources