Skip to content

Commit a562e49

Browse files
committed
Commerce pricing queries
1 parent 04f35ee commit a562e49

File tree

1 file changed

+46
-2
lines changed

1 file changed

+46
-2
lines changed

docs/commerce/5.x/system/pricing-rules.md

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ Pricing rules are sets of [conditions](#conditions) and [actions](#actions) that
66
The catalog pricing system replaces [sales](sales.md) from earlier versions of Commerce. If you upgraded your store from 4.x to 5.x and had already configured one or more sales, you will retain access to them—but we strongly recommend migrating your sales into pricing rules.
77
:::
88

9-
Catalog pricing is computed and stored in the database when its underlying data changes. Prices can also be manually refreshed at any time (or on a schedule) with the `commerce/pricing-catalog/generate` console command.
9+
Catalog pricing is computed and stored in the database when the underlying data changes. Prices can also be manually refreshed at any time (or on a schedule) with the `commerce/pricing-catalog/generate` console command. The resulting pricing information is joined in with normal variant queries and available transparently on the returned elements—even when rules produce customer-specific pricing!
1010

11-
The resulting pricing information is joined in with normal variant queries and available transparently on the returned elements—even when rules produce customer-specific pricing!
11+
This also means that you can accurately [sort and filter](#pricing-queries) based on effective prices in variant queries.
1212

1313
To create a pricing rule, visit <Journey path="Commerce, Store Management, Pricing Rules" />, and click **New catalog pricing rule**.
1414

@@ -98,3 +98,47 @@ A guest would pay <del>{{ myVariant.price|commerceCurrency }}</del> {{ myVariant
9898
The pricing catalog and discounts both operate on preestablished criteria and actions. In situations where a price needs to be dynamically calculated (say, from customer input in the form of [custom fields](orders-carts.md#field-layout) or [line item options](../development/cart.md#line-item-options-and-notes)), Commerce gives you complete control over pricing and cart contents via its [events system](../extend/events.md).
9999

100100
Read about [dynamically changing line item prices](kb:dynamically-customizing-line-item-prices) in the Knowledge Base.
101+
102+
## Querying Prices
103+
104+
Price data is always returned when executing [variant queries](products-variants.md#querying-variants). You can order any query by price, like this:
105+
106+
```twig{6}
107+
{# Fetch variants featured in a given article: #}
108+
{% set variants = craft.variants()
109+
.relatedTo({
110+
sourceElement: entry,
111+
})
112+
.orderBy('price DESC')
113+
.all() %}
114+
```
115+
116+
Substitute `promotionalPrice` or `salePrice` to explicitly order by those resolved values.
117+
118+
::: warning
119+
In general, `salePrice` will produce the most reliable and useful sorting. When using `promotionalPrice`, variants with no base promotional price defined will sort based on your database’s handling of `null` values, and may require an additional criteria like `['promotionalPrice IS NOT NULL', 'promotionalPrice DESC']` or `promotionalPrice DESC NULLS LAST` to ensure they appear at the end (or beginning) of the results.
120+
:::
121+
122+
Variants can also be filtered by price, using values compatible with <craft5:craft\helpers\Db::parseNumericParam()>:
123+
124+
```twig
125+
{% set dollarOrLess = craft.variants()
126+
.price('<= 1')
127+
.all() %}
128+
```
129+
130+
To return variants with effective promotional pricing (a promotional price less than its base price), use the `.onPromotion()` query param:
131+
132+
```twig
133+
{% set saleItems = craft.variants()
134+
.onPromotion(true)
135+
.all() %}
136+
```
137+
138+
::: tip
139+
See our [Listing Products On Sale](kb:listing-products-on-sale) article for some examples of advanced price-based queries.
140+
:::
141+
142+
This behavior approximates variants’ `.getOnPromotion()` method, but performs the comparisons directly in the database (rather than loading every variant and comparing them in memory)!
143+
144+
It is currently only possible to directly sort and filter _products_ by their default variant’s price—but you can always use the [`hasVariant` param](products-variants.md#product-hasvariant) to build more sophisticated variant-based criteria.

0 commit comments

Comments
 (0)