Getting collection using a assign variable liquid

Shopify Partner
221 1 19

Hello People,

Im trying to get a collection id based on a assign variable using the product type as a variable. Im making this from the product page. Here is the code:

{% assign productType = product.type | handle %}
{% assign collectionSelect = collections.productType | first %}
{{ collectionSelect.id }}

I assign the product type hanlde to a variable and try to call the collection with that name. On our site we have collections for all product types, with the exact same names. How could I make this work?

Entrepreneur, Developer, Geek, Gamer and very passionate about WEB development! :P

Need help with your Shopify Store? Contact me: jfeuchter@gmail.com :D
0 Likes
Shopify Partner
221 1 19

Lol I just found out how :P Ill leave this here:

{{ collections.[product.type].id }}

One line of code lol. I was afraid this would not work cause the product.type is not on handle form, but appearantly the server does see it as a handle. :P

Entrepreneur, Developer, Geek, Gamer and very passionate about WEB development! :P

Need help with your Shopify Store? Contact me: jfeuchter@gmail.com :D
0 Likes
Shopify Expert
9800 91 1553

To be slightly picky, drop the dot before the square bracket.

{{ collections[product.type].id }}

Now if your product type is "Apple Pie" I would not have expected that code to work. In that case you'd do something more like this:

{% assign productType = product.type | handle %}
{{ collections[productType].id }}

 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
1 Like
Shopify Partner
221 1 19

Hey Jason!

So right you are! :D hehe Thanks a bunch for the correction :P 

Entrepreneur, Developer, Geek, Gamer and very passionate about WEB development! :P

Need help with your Shopify Store? Contact me: jfeuchter@gmail.com :D
0 Likes
Excursionist
22 0 5

It seems like this code has suddenly stopped working. 

I'm getting this error: Liquid error: comparison of String with 1 failed

From this code: 
 

{% assign artist_handle = product.vendor | handleize %}

{% if collections[artist_handle].products_count > 1 %}

...some code...

{% endif %}
 

 

0 Likes
Excursionist
22 0 5

Anyone know how to solve this? 

0 Likes
Shopify Partner
221 1 19

After some research the problem might be that the .products_count is a string. I would sugget tryin this out:

{% assign artist_handle = product.vendor | handleize %}
{% assign products_count = collections[artist_handle].products_count | times: 1 %}

{% if products_count  > 1 %}

...some code...

{% endif %}

if that does not work, see what " products_count = collections[artist_handle].products_count" is bringing back by printing it like:

<!-- Products Count {{ products_count = collections[artist_handle].products_count }}-->

and debug from there :P tell me if it works 

Good Luck 

cheers! :D

Entrepreneur, Developer, Geek, Gamer and very passionate about WEB development! :P

Need help with your Shopify Store? Contact me: jfeuchter@gmail.com :D
1 Like
New Member
1 0 0

The above solution worked for me when I was having a similar error message.

0 Likes