Why i show {{collection.rules}} i can see the rules, but can’t acces it in liquid? I need rules to implement a development but shopify, but as always, shopify is difficult to extend and implement specific features.
The collection object has the following attributes
There is no rules attribute inside collection object
Yes, but if you show a json of object collection, you can view rules, anywhere if i need the rules can’t acces it? …
I too am having this same issue and would be happy about a solution / workaround. How do we get the “rules” array out of the “collection” object, that is output when calling {{ collection | json }} ? Thanks for any help.
The collection object has the following attributes
There are no rules attribute inside the collection object.
When you use {{ collection | json }} then it will return JSON format data of that particular collection.
Hey, Akshay,
Thanks but I actually looked at the documentation and are still not completely satisfied.
{{ collection | json }}
returns
{
"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"
}
in my case. While
{{ collection.rules | json }}
returns
null
I could save the json string in a new variable but would need to parse out all (or most) of the " characters. So this would be a quite messy solution. The problem is that despite not being mentioned in the object documentation and accessing it directly does not work, the “rules” object is present in the template but can not be used.
Thank you for sharing the code with me. For your information, Shopify is not giving access to each and every code. Some part of JSON code is used for admin purpose that is not accessible in the frontend or inside theme liquid. However, you can use the below code sample as a supported Collection JSON and its value is accessible for all the places. I hope you got all the information.
{
"collection":{
"id":160379633735,
"title":"collection name",
"handle":"collection-handle",
"description":"collection description",
"published_at":"2020-04-17T05:45:28-04:00",
"updated_at":"2020-04-17T05:46:06-04:00",
"image":null,
"products_count":13
}
}
Hey Akshay**,**
having certain fields restricted to admin use sounds reasonable. I will try working around this.
Thanks for your answers.
They don’t give you access to rules but if it comes out in {{collection | json}} is stupid. You can always create a mapper by trying {{collection | json}} as a string.
I spent a bit of time working on this to get collections to correctly display variant’s in my grid. Here’s some sample code to yank the conditions out of the rules (a similar approach can be used to effectively get the columns and relations)
{% liquid
assign bsHTGASRL = collection | json | split: ',"rules":' | last| split: ']' | first | append: ']' | split: '"condition":'
assign bsHackToGetAroundShopifyRulesLockout = ''
for condition in bsHTGASRL
if forloop.index0 != 0
assign temp = condition | split: '"'
assign bsHackToGetAroundShopifyRulesLockout = bsHackToGetAroundShopifyRulesLockout | append: temp[1] | append:","
endif
endfor
assign bsHackToGetAroundShopifyRulesLockout = bsHackToGetAroundShopifyRulesLockout | remove_last: ',' | split: ','
%}
This snippet effectively turns the json dump above from @Simon_Brandt into a simple array of
[
"black",
"pattern"
]
YMMV, it’s jank code.
My goodness, what is the point in denying access to particular values if it can be worked around anyway? It just encourages horrific workarounds like this which (thank you StarbucksRita) I’m now forced to use to get the behaviour I want.
Making collection.rules visible via the json filter but invisible by direct access does not make sense, and “admin purposes” is complete nonsense. Either it being visible via the json filter is a bug, or it not being accessible via collection.rules is a bug.