Search code examples
shopifyliquidshopify-template

Liquid - Nested "where" filter


Is it possible to use the | where filter on an array such as a cart line item object to filter by property value.

For example, I would like to find all line items with property._design_id of 123. As far as I can see you're able to filter array | 'type', 'Coat', but not something like array | 'properties._design_id', '123'.

I am aware that I could alternatively do this with a for loop & if statements, however that's a few more lines of code & I would be able to reuse the array without recomputing each time I needed to use those items.


Solution

  • This currently seems impossible as liquid compares exact keys & values not keys for nested arrays.

    https://github.com/Shopify/liquid/blob/f1846d63a32f006cddf4b2d968ef4e552a9ccc94/lib/liquid/standardfilters.rb#L411

    Current work around is:

    for item in cart.items
        if item.properties._design_id === '123'
             code...
        endif
    endfor