|
e387fc0d570e312768af3763c49051c0
|
List all Types in a Collection without duplicatesby esdot - Member - 05:33PM, Aug 05, 2008 |
|
I’m trying to get a hyperlink list of all types within a collection. It seems this could be done by looping through all products, for each product create a link using its type only if it has a new type (thereby eliminating a single type being repeated once or more). The question is, how do I do that? One approach is to create an array that would hold all the types that have appeared in the collection at any given iteration, and compare the current iteration (product type) against all types in the array. Can you create arrays? If you can’t you could probably put all types in a string variable while separating each type with a special character like an underscore. Then you would need to access the String objects indexOf and subStr methods, do those exist? This is all I have so far that successfully outputs a list of types each as a hyperlink for a single collection. |
|
|
here’s some code that works pretty well for me. the one downfall of this method is that if products in the collection aren’t ordered by type, then you get some duplicates. for example if the first product is type A second is type B and third is type A, then type A will show up twice. If liquid had the ability to create arrays it would be easy, but it doesn’t (someone patch that!) Anyway, here is my code:
---
Owner, Velocis Enterprises LLC We Build Custom Shopify Themes - www.featurefy.com .::. Applications for Shopify
|
|
|
Kids, I’ve got some good news. I’ve managed to string together a method that can loop through any collection and skip duplicates, independently of the ordering. With future upgrades to liquid (i.e. concatenation operator for variables, ability to use filters within {% %} tags, etc.) the code would look simpler. Basically, as you loop through, a string containing all product types is created. each product type is checked against it – if the type is found in the list, that means it’s been output already. Say the complete list of types returned is: This is what happens: At each pass, the code tries to replace the type (e.g.’,beds,’) with ’’ in the list. if this is successful, the product list is now shorter (i.e. sofas,tables,). I then compare it with the list as it was before the pass to know if I have a hit. if I don’t have a hit, I can add the type to the list. code: Try it out, the same approach should work with any other collection. hope this helps. —
|
|
|
Well – I did not realise there was a “contains” operator. makes things a lot more simple. here is the updated code.
—
|
|
|
Oliver, good stuff!! I see the capture class in the liquid documentation but there is no explanation as to what it does or how it functions. Can you elaborate on that? ---
Owner, Velocis Enterprises LLC We Build Custom Shopify Themes - www.featurefy.com .::. Applications for Shopify
|
|
|
capture works like assign, but is often more powerful, because you can generate a user variable based on liquid output + basic strings, etc. And this is very handy, by the way. Thanks Oliver!
|
|
|
Merci, Olivier, that’s very clever! ---
http://wiki.shopify.com/Eleven_heavens Shopify Theme Work mllegeorgesand AT gmail DOT com — Last edited 06:18AM, Sep 05, 2008 |
|
|
That’s good stuff! If I knew how to make a new section on the wiki for code snippets (It needs to be there) then I would add this and several others I have used and collected. That and there is no section for operators on the wiki at all. ---
evolution design
|
|
|
Hello, I’m trying to implement Olivier’s code above, but I’m getting this error:
Here’s my exact code below – any help would be much appreciated – I’m new to shopify/liquid and a bit out of my depth. ; )
— Last edited 04:16AM, Sep 18, 2008 |
|
|
Well for one thing you use the keyword ‘for’ twice. ---
http://wiki.shopify.com/Eleven_heavens Shopify Theme Work mllegeorgesand AT gmail DOT com
|
|
|
Nice catch Caroline. :) That was a copy/paste error when I was posting to the forum…I accidentally deleted the first few lines and had to retype…must have had butter on my fingers. Fixed it in my original post. Anyhow, I just double checked, and the code in my page doesn’t have the duplicate “for”. I still haven’t had any luck fixing the error. Any ideas? — Last edited 04:32AM, Sep 18, 2008 |
|
|
The collection name should be the handle I think. No capital letter. ---
http://wiki.shopify.com/Eleven_heavens Shopify Theme Work mllegeorgesand AT gmail DOT com
|
|
|
Yes it has to be the handle… Provided ‘accessories’ is the handle…
Or
Since Liquid is not recognizing what you give it as the name of a collection or the name of any other ‘property’ of collections, it just tells you so…
http://wiki.shopify.com/Collections ---
http://wiki.shopify.com/Eleven_heavens Shopify Theme Work mllegeorgesand AT gmail DOT com — Last edited 05:30AM, Sep 18, 2008 |
|
|
Yep. That was it. Thank you so much for the help. Cheers!
|
|
|
Question about a variation of this: What if I wanted to create a table (instead of list) of just the first image of the first product for each type? I figured out how to get the first image by using: instead of: But I’m not sure how to get it all into a table. Any help would be appreciated.
|
|
|
Hello All, I’m still pounding my head against the desk with this one. I’m not a programmer, and I’ve just starting using liquid, so I know I’m in over my head. : ) Cheers!
|
|
|
Why do you want to use a table for this? Using CSS you can style your list so that images will be side by side with x images per ‘row’. With the right amount of margin you want. You cannot use tablerow in this particular case. This falls into the category of “will produce swiss cheese table”. Just write your own markup. If one image per row, you go…
---
http://wiki.shopify.com/Eleven_heavens Shopify Theme Work mllegeorgesand AT gmail DOT com — Last edited 03:19PM, Sep 19, 2008 |
|
|
Thank You Caroline! You’re definitely right about the “swiss cheese table” – that was the only result I could get. I guess there was no reason to use tablerow – other than I found an collection.liquid page someone else made that used tablerow – so I though it might be a good method. Usually I wouldn’t even consider tables for this. Thanks again for the help. Much appreciated. : )
|
|
|
You are welcome :-) ---
http://wiki.shopify.com/Eleven_heavens Shopify Theme Work mllegeorgesand AT gmail DOT com
|
|
|
I wish I had a clue as to what any of that means! :(
|
|
|
Joanne, Does it sound like something that would be useful to you? If so, I can help you understand the basics of why you might want the above code – although I’m very much still learning myself. Say you have a collection of products called “Shirts”. In that collection you have several product-types “Men, Women, Kids, Babies”. Each of those product-types could have a lot of products assigned to them. The code that So in your theme.liquid page the code will produce a list like this:
Hope that helps – someone please correct me if I explained that wrong or left anything out. My understanding of what’s going on is fragile at best. ; )
|
|
|
I just have to point out here that these and many other things rely on all_products, which is actually capped at the first 50 products. So if you are trying to implement this for a larger shop, these sorts of solutions don’t really work unless you can reliably only iterate over collections with 50 or fewer products. It’ll work GREAT for a lot of smaller shops though.
|
|
|
On my default home page is the typical blurb about welcome to the site. I need to add a menu to help narrow a customers searches. The code here is very interesting but I have more than 50 products! Multiple links to numerous categories/collections, each complete and each unique with no duplicates is what I need but thought there might be a simpler method. I have 12 custom collections already each with x products but none have their own url as I can see? Do I need to add the collections code to generate the links or can I create a simpler link to each existing Custom Collection? How do I create a link to a collection using custom collections, isn’t that what it’s for? — Last edited 03:43PM, Mar 02, 2009 |
|
|
Sounds like you need to make a simple collection. See wiki and Here To get directly to your collection simply add the handleized name behind collections like so: /collections/my_custom_collection ---
evolution design
|

Bradley Joyce
Member
08:07PM, Aug 08, 2008