Looping and Filtering with Liquid (or JavaScript)

Highlighted
New Member
1 0 0

I'm new to Shopify and I'd like to know how to loop and filter through all the products in my collection object to remove the sale price for sold out items.

 

Is this possible using Liquid? I know the basics after taking a crash course on YouTube but I don't know in which file I would apply this logic. The whole Shopify landscape is still a bit foreign to me.

 

I've tried to do something similar before in Shopify with JavaScript. But my code didn't work, despite being correct. I heard it's not as simple to apply JavaScript in Shopify.

 

If what I'm trying to do can't be done with Liquid, can someone tell me how and where to use JavaScript in this situation?

 

Thank you!

 

0 Likes
Highlighted
New Member
3 0 0

The liquid is an open-source template language created by Shopify and written in Ruby. It is the backbone of Shopify themes and is used to load dynamic content on storefronts. Liquid has been in production use at Shopify since 2006 and is now used by many other hosted web applications.

Now to your question, to filter Object in JavaScript, you can use the JavaScript array filter() method, and based on your condition, you can filter out the specific data and it will return the array which satisfies your condition.

The filter() method is a pure function and does not change the original array instead it will return a new array that satisfies the requirement of the provided function in the argument.

Using filter(), you can filter out unnecessary data.

I hope this helps.

0 Likes