Access collections rules / conditions object through liquid

kevinyeh
New Member
3 0 1

When accessing the products object, there's the collections array where it shows all the collections where the product is in. Within each collection object, I see a rules field that has the condition(s) which the collection is using to filter the products. I can't seem to access that 'rules' field through liquid, it returns as null. Anyone with some idea why I can't / what I can do to access the conditions where the collections are using? Thanks!

Hi kevinyeh, could you post some screenshots? I don't see a rules attribute in the collections object. (https://help.shopify.com/en/themes/liquid/objects/collection).

 

 

Making Shopify Tasks Straight Forward. Contact me directly to get help with your Shopify website quickly and painlessly.

Website: https://theshoptinkerers.com/
0 Likes
Simon_Brandt
Tourist
4 0 1

I´m having the same issue right now.

 

My "var dump"

{{ collection | json }}

outputs the following object:

{
   "id":174687682605,
   "handle":"black-pattern",
   "title":"Black Pattern",
   "updated_at":"2020-04-14T11:25:42+02:00",
   "body_html":"",
   "published_at":"2020-02-13T11:28:22+01:00",
   "sort_order":"best-selling",
   "template_suffix":"",
   "disjunctive":false,
   "rules":[
      {
         "column":"variant_title",
         "relation":"contains",
         "condition":"black"
      },
      {
         "column":"variant_title",
         "relation":"contains",
         "condition":"pattern"
      }
   ],
   "published_scope":"web"
}

 

I need to perform further filtering based on those rules near the end. However {{ collection.rules | json }} returns null. Everything else I can think of would be a very hacky solution at best.

Thanks for any hints.

jpagan
Tourist
6 0 3

As a new dev that's recently started working with Shopify, it's really frustrating to find questions like this that have gone unanswered for months. Would be great if shopify staff could monitor their own forum and provide answers...

It's obvious that the information is there but for some reason attempting to access anything under the rules param is denied

0 Likes
DaveRS
Tourist
9 0 4

Count me +1 for hoping for an answer to this.  I'm trying to make the Filter By a bit smarter to exclude tags that are already captured by the collection conditions where Product Tag is equal to.  It makes no sense to repeat those tags in the Filter By field.  If we could read the collections rules, we could skip that tag when looping through to populate the Filters tags.

0 Likes
DaveRS
Tourist
9 0 4

A server side json parser would be better, but in lieu of that, I managed to cobble together a bit of client side javascript for my needs, which was to parse the collections json for specific rules on the tag column and use that to remove matching tags from the filter list.  Here is the code I added to my collections-template.liquid file.

The div tag has the conditions object as json and is hidden.

The script is a bit of javascript and jquery to parse the json text into an object and then uses that to find every matching option in the BrowseBy select field (aka the Filter by drop down) and remove it.  If there is only 1 option left (which is the default "Filter By" option), the find step is hide the drop down completely.

 

 

<div id="condition-tags" style="display: none;">{{ collection | json }}</div>

<script type="text/javascript">       
  $(document).ready(function() {
    const obj = JSON.parse($("#condition-tags").text());  	    
    if (obj)
    {         	
      const rules = obj.rules;      	
      if (rules)
      {          
        const reduced_rules = obj.rules.filter(i => i.column == "tag" && i.relation == "equals").map(i => i.condition);    
    	reduced_rules.map(s => $("#BrowseBy option:contains(" + s + ")").remove());
        if ($("#BrowseBy option").length <= 1)
        {
          $("#BrowseBy").hide();
        }  
      }
    }
  });
</script>