Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
Hi there,
I would like to do filtering of my collections by metafields in liquid. How should I do it?
If I use the where in collection predefined fields, I can do it like this. {% assign collections = collections | where: 'description', 'promotion' %}
But while I am trying to do query on the 'group' metafield I have created, it simply not working.
{% assign collections = collections | where: 'metafields.custom.group', 'promotion' %}
I tried to use this solution (https://community.shopify.com/c/technical-q-a/filtering-collections-by-metafields-using-where/m-p/18...) and surf on the Internet and can get no useful information.
Please help Thanks
Solved! Go to the solution
This is an accepted solution.
I think the where in liquid won't be able to support a dot operator to retrieve nested array value.
The following is the definition of where in ruby. Unless array in Ruby support dot operator property value like 'metafields.custom.group', it cannot support for what I mentioned.
# @liquid_public_docs
# @liquid_type filter
# @liquid_category array
# @liquid_summary
# Filters an array to include only items with a specific property value.
# @liquid_description
# This requires you to provide both the property name and the associated value.
# @liquid_syntax array | where: string, string
# @liquid_return [array[untyped]]
def where(input, property, target_value = nil)
ary = InputIterator.new(input, context)
if ary.empty?
[]
elsif target_value.nil?
ary.select do |item|
item[property]
rescue TypeError
raise_property_error(property)
rescue NoMethodError
return nil unless item.respond_to?(:[])
raise
end
else
ary.select do |item|
item[property] == target_value
rescue TypeError
raise_property_error(property)
rescue NoMethodError
return nil unless item.respond_to?(:[])
raise
end
end
end
This is an accepted solution.
I think the where in liquid won't be able to support a dot operator to retrieve nested array value.
The following is the definition of where in ruby. Unless array in Ruby support dot operator property value like 'metafields.custom.group', it cannot support for what I mentioned.
# @liquid_public_docs
# @liquid_type filter
# @liquid_category array
# @liquid_summary
# Filters an array to include only items with a specific property value.
# @liquid_description
# This requires you to provide both the property name and the associated value.
# @liquid_syntax array | where: string, string
# @liquid_return [array[untyped]]
def where(input, property, target_value = nil)
ary = InputIterator.new(input, context)
if ary.empty?
[]
elsif target_value.nil?
ary.select do |item|
item[property]
rescue TypeError
raise_property_error(property)
rescue NoMethodError
return nil unless item.respond_to?(:[])
raise
end
else
ary.select do |item|
item[property] == target_value
rescue TypeError
raise_property_error(property)
rescue NoMethodError
return nil unless item.respond_to?(:[])
raise
end
end
end
Hey Community! As the holiday season unfolds, we want to extend heartfelt thanks to a...
By JasonH Dec 6, 2024Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024