for loop - Unable to get my product ids after constructing the handle in Shopify
I've been hurting an issue since hours. I hope someone will be able to understand my issue and help if possible. I'll try to be as clear as I can :
I need to show all the reviews of all my product within their collection. My collections are constructed like this : They show only the PARENT products. To show you an exemple, all my parents product are constructed like this :
Parent's product handle : parenthandle
All my parents product have 2 child products constructed like this :
Child's product handle - tshirt man : parenthandle-tshirt-man
Child's product handle - tshirt woman : parenthandle-tshirt-woman
To be able to show my Parent product reviews, I need to concat its child's product ids as only my child's product have reviews (thats the way my reviews app is made).
So, now that you have the context, in my collection page, I constructed something like this :
{% assign producthandles_homme = "" %}
{% assign producthandles_femme = "" %}
{% assign id_enfant_homme = "" %}
{% assign id_enfant_femme = "" %}
{% assign allinone = "" %}
{% for product in collection.products %}
// Here I'm gonna construct my child products handle from their parents handle, to be able to then get their ids
{% assign producthandles_homme = producthandles_homme | append: product.handle | append: '-tshirt-homme' %}
{% assign producthandles_femme = producthandles_femme | append: product.handle | append: '-tshirt-femme' %}
// Now I can get their id as I constructed their handle
{% assign id_enfant_homme = all_products[producthandles_homme].id | append: product.id | append: ',' %}
{% assign id_enfant_femme = all_products[producthandles_femme].id | append: product.id | append: ',' %}
// Here what I want to do is to concat all the ids of all my child product to be able to display their reviews
{% assign allinone = allinone | append: id_enfant_homme | append: id_enfant_femme %}
{% endfor %}
// Finally here I want to display them all in "data-product-id"
<div class="avis-verifies-etoiles-collection">
<div class="NETREVIEWS_PRODUCT_STARS" data-product-id="{{ allinone }}"></div>
</div>
Unfortunately, it does not work, and I can't understand why. Sometimes it repeats the same id, sometimes no, all kind of ids are displaying :
Here is the screenshot of the result (https://i.stack.imgur.com/7SLek.png)
Does someone would be able to help me ? I hope I was clear!
Thank you in advance!!! Coline
Answer
Solution:
Try to replace yourfor
loop with the following:
{%- liquid
for product in collection.products
assign producthandles_homme = product.handle | append: '-tshirt-homme'
assign producthandles_femme = product.handle | append: '-tshirt-femme'
if all_products[producthandles_homme] != blank
assign allinone = allinone | append: ',' | append: all_products[producthandles_homme].id
endif
if all_products[producthandles_femme] != blank
assign allinone = allinone | append: ',' | append: all_products[producthandles_femme].id
endif
endfor
-%}
Source
Didn't find the answer?
Our community is visited by hundreds of Shopify development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.