Skip to content

Commit

Permalink
switch sorting filters
Browse files Browse the repository at this point in the history
  • Loading branch information
tekhaus committed Feb 4, 2025
1 parent 391c944 commit e62dcce
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
3 changes: 2 additions & 1 deletion docs/auto-sort-collections-by-product-properties/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ This task re-sorts your collections by the product property, product metafield,
"first_variant_property": "",
"only_sort_these_collections__array": null,
"reverse_sort__boolean": false,
"sort_naturally__boolean": false,
"run_hourly__boolean": false,
"run_daily__boolean": false
}
Expand All @@ -43,7 +44,7 @@ user/collection_sort/complete

This task re-sorts your collections by the product property, product metafield, or variant property that you choose. Use the "Product property" or "First variant property" options to control what attribute the task looks up. For example, using `publishedAt` in the "Product property" field will result in sorting by the date and time the product was published, while using `sku` in the "First variant property" field will result in sorting by the sku of the first variant of each product in the collection. Alternatively, enter a product metafield as "namespace.key" (e.g. `store.priority`), and the task will attempt to sort by the value of that metafield.

Run this task manually to re-sort your collections on demand, or choose to run it hourly or nightly. This task will scan all collections in the shop on each run, unless you configure it to only sort certain collections using each collection's title, handle, or ID. Optionally, choose the "Reverse sort" option to have the results reversed, mainly useful for sorting by descending numeric values (e.g. `inventoryTotal`).
Run this task manually to re-sort your collections on demand, or choose to run it hourly or nightly. This task will scan all collections in the shop on each run, unless you configure it to only sort certain collections using each collection's title, handle, or ID. Optionally, choose the "Reverse sort" option to have the results reversed, mainly useful for sorting by descending numeric values (e.g. `inventoryTotal`). If the property/metafield value contains numeric strings, consider using the ["Sort naturally"](https://learn.mechanic.dev/platform/liquid/filters#sort_naturally) task option.

**Important:**

Expand Down
47 changes: 38 additions & 9 deletions docs/auto-sort-collections-by-product-properties/script.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
{% assign first_variant_property = options.first_variant_property %}
{% assign only_sort_these_collections = options.only_sort_these_collections__array %}
{% assign reverse_sort = options.reverse_sort__boolean %}
{% assign sort_naturally = options.sort_naturally__boolean %}

{% comment %}
-- build the query fragment for the configured property
Expand Down Expand Up @@ -256,27 +257,55 @@

{% comment %}
-- make sure this is always a serializable/sortable object, defaulting to nil in the case of an empty string
-- values which are *either* nil or an empty string will always end up at the end of the list
{% endcomment %}

{% assign product_id_and_value["value"] = value | json | parse_json | default: nil %}
{% assign product_ids_and_values[product_ids_and_values.size] = product_id_and_value %}
{% endfor %}

{% assign sorted_product_values = product_ids_and_values | sort: "value" %}
{% assign sorted_product_ids = sorted_product_values | map: "id" %}
{% assign current_sort_product_ids = product_ids_and_values | map: "id" %}

{% if reverse_sort %}
{% comment %}
-- only reverse the order of products which have values, leaving nil values at the end
{% endcomment %}
{% comment %}
-- use sort_natural filter so the sort is case-insensitive
-- use sort_naturally when option chosen to sort numeric strings as humans might expect
-- exclude null values from the sort since sort_naturally leaves them in front
{% endcomment %}

{% if sort_naturally %}
{% assign sorted_product_values
= product_ids_and_values
| where: "value"
| sort_naturally: "value"
%}

{% else %}
{% assign sorted_product_values
= product_ids_and_values
| where: "value"
| sort_natural: "value"
%}
{% endif %}

{% log sorted_product_values: sorted_product_values %}

{% comment %}
-- concatenate nil values to end of sorted IDs regardless of whether the sort is reversed or not
{% endcomment %}

{% if reverse_sort %}
{% assign sorted_product_ids
= sorted_product_values
| where: "value"
| map: "id"
| reverse
| concat: sorted_product_ids
| concat: current_sort_product_ids
| uniq
%}

{% else %}
{% assign sorted_product_ids
= sorted_product_values
| map: "id"
| concat: current_sort_product_ids
| uniq
%}
{% endif %}
Expand Down
Loading

0 comments on commit e62dcce

Please sign in to comment.