Is there a way to override the default product ordering on a collection page?

I’m looking to override the default product ordering on a collection page. Currently the default ordering is set to Best Selling and I’d like to switch this to Created-Descending. I know I can change this within the collection itself but I have a lot of collections and going through all of them manually would be extremely time consuming. Is there a way to ‘hard-code’ the sort order in a template. I don’t need to worry about it affected product order in other areas of the site. The sort order would always be Created-Descending.

I tried looking in the collections-template.liquid file in the site and found this line of code

"sortBy": {{ collection.sort_by | default: collection.default_sort_by | json }},

Changing the ‘collection.default.sort_by’ seemed to come through on the front end section settings but the order remained the same. Would I need to make changes elsewhere?

Any help would be much appreciated.

@ahopkins_1 ,

you might want to trigger a change on load by a script to be sure that sort by indeed sort just in case it is not

@Propero

Thank you for your quick reply

I see, I thought it might be something like this as when I viewed the source code of the page having changed the templates sort order, I could see my change but it was as if there was something else influencing the order.

Are you able to guide me on how to do this? I’m fairly new to Shopify so I’m not sure what would need to be written and where it would need to be placed to trigger.

@ahopkins_1

You can put the following code at the bottom of collection-template.liquid


hope this helps

@Propero

That’s great, thank you.

The ID that you’re referencing (#SortBy), is this referring to an element in the front or back end. By this I mean there is a dropdown within the collection in the dashboard where the order can be set. Is this what this ID is referencing?

@Propero

To clarify my last post, I should’ve asked, whereabouts can I find that ID that you are referencing in the script? That way I can determine whether my ID is different. Thank you

@ahopkins_1

I meant to say id of a select statement something like


You see id of the select element as "SortBy" . It could be different depending on theme you have

@Propero

Sorry for the delayed response.

So is this js trigger under the assumptions that there would be a select dropdown list for ordering is already being displayed on the front end. The script would then re-set the ordering selection on page load? But the select list would need to be present.