Collections sort by metafield 'year' / Minimalista

Collections sort by metafield 'year' / Minimalista

harry-w
Tourist
5 0 1

Hi, I'm using the Minimalista theme to make a website for an artist. I've added the metafield 'Year' to each product, to show when each artwork was made. In the drop down 'Sort by' menus, I would like the option of sorting collections by that metafield date, ascending and descending — as opposed to the default 'Date, old to new' (which is when the product data was entered and pretty much irrelevant in this case). Is this possible without purchasing an app?

Thanks in advance

 

Replies 3 (3)

rajweb
Shopify Partner
683 58 130

Hey @harry-w ,

Thank you for reaching out with your request regarding the Minimalista theme! Adding a custom sort option for collections based on the "Year" metafield is a fantastic idea, especially for showcasing artwork in a chronological order that aligns with your needs.

While Shopify doesn't natively allow sorting collections by custom metafields directly in the "Sort by" menu, we can absolutely achieve this without purchasing an app by implementing a custom solution. Here’s how:

1. Create a Custom Collection Template:

- We'll create a new collection template in the theme that includes a custom sort logic for your metafield Year.

- The metafield will be used to organize the products by date (ascending or descending) using liquid code.

2. Modify the Collection Page Code:

- We'll write custom Liquid code to retrieve the Year metafield for each product and sort the collection accordingly.

- This involves looping through the products in the collection and applying logic to sort by the Year metafield.

3. Add Dropdown Options for Sorting:

- I'll update the "Sort by" dropdown menu to include custom options for sorting by the Year metafield in ascending and descending order.

4. Ensure Performance and Usability:

- The code will be optimized to ensure smooth loading, even with large collections.

- We'll test the changes to make sure the dropdown works perfectly and displays your artwork as intended.

Would you like me to proceed with setting this up? If needed, I can also provide detailed instructions or collaborate further to tailor the solution to your specific requirements.

Looking forward to your response!

Best regards,

Rajat

-Need a Shopify developer?
https://rajatweb.dev/

Email: rajat.shopify@gmail.com
tim
Shopify Partner
4017 426 1486

And how would you handle pagination?

If my post is helpful, consider liking it -- it will help others with similar problem to find a solution.
I can be reached via e-mail tairli@yahoo.com
harry-w
Tourist
5 0 1

Hi Rajat — thanks for the reply, but I was hoping to do it myself. I may come back to you if I get stuck!