> ## Documentation Index
> Fetch the complete documentation index at: https://docs.uselayers.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Conditional sort expressions

> Gate a sort term with an Only when condition so products that don't qualify fall through to the next rule instead of ranking on an unreliable signal.

<Note>
  Conditional sort expressions are in **Beta**. The behavior and UI may still evolve based on merchant feedback.
</Note>

## Overview

A conditional sort expression is any value sort term — a metric, an attribute, or a [weighted group](/platform/sorting/weighted-groups) — with an **Only when…** condition attached. Products that satisfy the condition are ranked by the term as usual. Products that fail the condition are treated as having no value for that term and fall through to the **next** expression in the sort group.

This lets you rank on a signal only for the products where the signal is reliable, without hiding the rest of the catalog or writing a custom formula.

## When to use it

Use an **Only when…** condition when a sort signal is only meaningful for part of the catalog:

* **Confidence tiers on sparse metrics.** Sort by conversion rate only for products with enough orders to trust the number. Everything else falls through to a broader signal like views or overall sales.
* **Stacked tiers without a custom column.** Chain two or more conditional terms to build tiered ranking — top tier ranks by a strict signal, next tier by a looser signal — without introducing a computed attribute just to hold the tier key.
* **Attribute-scoped sorts.** Sort by a metafield or attribute that only some products carry (for example, `metafield.custom.editor_score`), letting unscored products slot in beneath scored ones instead of ranking as if they were zero.

For **request-level** targeting, use the existing **Conditions** panel on the expression or group instead. Conditions show or hide an entire expression based on request context like device, market, or applied filters. `Only when…` is per-product; **Conditions** is per-request.

## How the fall-through works

Each sort term contributes one ranking position. When a product fails an `Only when…` condition, the term yields no value for that product, so the product ties with every other failing product on that term. The next term below then breaks the tie.

Consider a sort order with two terms:

```text theme={null}
1. Sort by: Conversion rate — Descending, Only when: orders (30d) ≥ 25
2. Sort by: Views (30d) — Descending
```

* Products with **25+ orders in the last 30 days** are ranked by conversion rate first, then by views as a tiebreaker.
* Products with **fewer than 25 orders** all tie on the first term and are ranked by views.

Because the fall-through is a genuine tie on the gated term, the next term ranks the failing cohort against **itself** — you don't need to duplicate the signal or invent a placeholder value.

## Where the condition applies

`Only when…` is available on value sort terms:

* Simple attribute and metric expressions
* [Weighted groups](/platform/sorting/weighted-groups) (gates the whole blended score)
* [Soft boost](/platform/sorting/soft-boost) already supports its own per-product condition — see that page for details

`Only when…` does **not** apply to [priority rules](/platform/sorting/priority-rules). Priority rules already use a condition to decide who gets promoted or demoted, so a second gate would be redundant.

## Configure an Only when condition

1. Open a sort order and expand the expression card you want to gate.
2. Open the expression's settings and enable **Only when…**.
3. Add one or more rules. Each rule references a product field — either a metric or a product attribute — with an operator and a value.
4. Use the group combinator (**AND** / **OR**) to combine multiple rules.
5. Save and [preview](/platform/sorting/preview) the sort order to confirm the fall-through order is what you expect.

The rule builder uses the same operator set as [priority rules](/platform/sorting/priority-rules), including numeric comparisons (`greater than`, `between`), text matches, null checks (`is null` / `is not null`), and list operators (`in` / `not in`).

## Examples

### Rank by conversion rate only for products with enough orders

```text theme={null}
1. Sort by: Conversion rate (30d) — Descending
     Only when: orders (30d) ≥ 25
2. Sort by: Views (30d) — Descending
```

Products under the 25-order threshold don't rank on a noisy conversion number — they rank on views instead. Trusted performers still lead.

### Stack confidence tiers

```text theme={null}
1. Sort by: Revenue per view (30d) — Descending
     Only when: views (30d) ≥ 500
2. Sort by: Revenue per view (30d) — Descending
     Only when: views (30d) ≥ 100
3. Sort by: Sales (30d) — Descending
```

Products with 500+ views form the top tier, 100–499 views form the middle tier, and the rest fall through to raw sales — no custom tier column required.

### Rank by a metafield only when it's set

```text theme={null}
1. Sort by: metafield.custom.editor_score — Descending
     Only when: metafield.custom.editor_score is not null
2. Sort by: Sales (30d) — Descending
```

Editor-picked products rank by their score; everything else ranks by sales.

## Best practices

* **Preview after adding.** Fall-through order is easy to reason about once you see it laid out — [preview the sort order](/platform/sorting/preview) with sort effect annotations to confirm which term each product ranked on.
* **Keep conditions simple.** One or two rules per term is usually enough. Complex nested conditions become hard to reason about later.
* **Prefer a threshold over `is not null` for metrics.** Metric values default to zero when there's no data, so gate them with a numeric threshold (`orders ≥ 25`) rather than a null check.
* **Order matters.** Put the strictest gated term first and the fallback term last. The pipeline reads top-down; each falling-through cohort meets the next term in turn.

## See also

* [Priority rules](/platform/sorting/priority-rules)
* [Weighted groups](/platform/sorting/weighted-groups)
* [Soft boost](/platform/sorting/soft-boost)
* [Preview a sort order](/platform/sorting/preview)
