How can I modify the 'collection' object in Shopify?

I am new to Shopify and have just started learning it. I was going through the docs and I found out that there is a ‘collection’ object that has many useful properties inside it like products, sort_options etc. As I was looking into the source code of my theme, I found many occurrences of this ‘collection’ object but I could not find the file in which this object is defined. So my question is that can we edit this ‘collection object’ and if yes then how?

Just as an example, suppose if I want to add a sort option, my first instinct was to add a string on collection.sort_options array but where is this array defined?

link of ‘collections’ object in documentation -

‘Collection’ object

The collection object is a Liquid object created automatically by Shopify and can be accessed, along with its properties (in read-only mode) in the Theme’s code using the Shopify Liquid template language.

Back to your questions about the possibility to edit the collection object, the answer is no, you cannot edit a collection object in the theme code. The collection object passed to you by Shopify engine on the front end is read-only and immutable.

The same applies to the sort_options property of the collection object that contains only the Sort Options natively available on Shopify collections.

If you want to add different Sorting Options (not available in Shopify) on your collection pages you need to develop some custom code in the frontend (that could be done for collections with a limited number of items) or by relying on 3rd-party apps or custom apps.

1 Like