Skip to content
This repository was archived by the owner on Nov 19, 2024. It is now read-only.

Commit 31f70fd

Browse files
authored
GraphQL: Custom sort attributes (#6468)
* GraphQL: Custom sort attributes * linting error
1 parent b119352 commit 31f70fd

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

src/guides/v2.3/graphql/queries/products.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,8 @@ Attribute | Data type | Description
133133
{:.bs-callout-info}
134134
If you use MySQL for searches and you specify `relevance` and another sorting attribute, the `relevance` results are always listed first. This limitation does not apply to [Elasticsearch]({{page.baseurl}}/config-guide/elasticsearch/configure-magento.html).
135135

136+
To enable sorting by an attribute that is not in the `ProductAttributeSortInput` object, set the **Stores** > Attributes > **Product** > <Attribute Name> > **Storefront Properties** > **Use in Search** and **Used in Sorting in Product Listing** fields to Yes.
137+
136138
## Deprecated input attributes
137139

138140
The `filter` and `sort` attributes require new input objects. The following sections list the deprecated attributes.
@@ -1021,6 +1023,78 @@ In the following example, a catalog price rule that provides a 10% discount on a
10211023
}
10221024
```
10231025

1026+
### Sort by a custom attribute
1027+
1028+
In this example, the `description` attribute has been enabled by setting the **Stores** > Attributes > **Product** > description > **Storefront Properties** > **Use in Search** and **Used in Sorting in Product Listing** fields to Yes. The query returns all products with a price range of $28 to $30, sorted by the description.
1029+
1030+
**Request:**
1031+
1032+
```graphql
1033+
{
1034+
products(filter: { price: { from: "28" to: "30"} }
1035+
sort: {
1036+
description: ASC
1037+
}) {
1038+
total_count
1039+
items {
1040+
name
1041+
sku
1042+
description {
1043+
html
1044+
}
1045+
price_range {
1046+
maximum_price {
1047+
regular_price {
1048+
value
1049+
}
1050+
}
1051+
}
1052+
}
1053+
}
1054+
}
1055+
```
1056+
1057+
**Response:**
1058+
1059+
```json
1060+
{
1061+
"data": {
1062+
"products": {
1063+
"total_count": 25,
1064+
"items": [
1065+
{
1066+
"name": "Erikssen CoolTech&trade; Fitness Tank",
1067+
"sku": "MT01",
1068+
"description": {
1069+
"html": "<p>A good running tank helps make the miles pass by keep you cool. The Erikssen CoolTech&trade; Fitness Tank completes that mission, with performance fabric engineered to wick perspiration and promote airflow.</p>\n<p>&bull; Red performance tank.<br />&bull; Slight scoop neckline. <br />&bull; Reflectivity. <br />&bull; Machine wash/dry.</p>"
1070+
},
1071+
"price_range": {
1072+
"maximum_price": {
1073+
"regular_price": {
1074+
"value": 29
1075+
}
1076+
}
1077+
}
1078+
},
1079+
{
1080+
"name": "Primo Endurance Tank",
1081+
"sku": "MT03",
1082+
"description": {
1083+
"html": "<p>Chances are your workout goes beyond free weights, which is why the Primo Endurance Tank employs maximum versatility. Run, lift or play ball &ndash; this breathable mesh top will keep you cool during all your activities.</p>\n<p>&bull; Red heather tank with gray pocket.<br />&bull; Chafe-resistant flatlock seams. <br />&bull; Relaxed fit. <br />&bull; Contrast topstitching.<br />&bull; Machine wash/dry.</p>"
1084+
},
1085+
"price_range": {
1086+
"maximum_price": {
1087+
"regular_price": {
1088+
"value": 29
1089+
}
1090+
}
1091+
}
1092+
},
1093+
...
1094+
}
1095+
}
1096+
}
1097+
```
10241098
### Retrieve related products, up-sells, and cross-sells
10251099

10261100
The following query shows how to get related products, up-sells, and cross-sells for a product:

0 commit comments

Comments
 (0)