Skip to content

Saleor Core: the high performance, composable, headless commerce API.

Added a search position field for the storefront navigation

NyanKiyoshi51f9cddec0a9
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 68886f8343..aad7a3e62f 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -17,6 +17,7 @@ All notable, unreleased changes to this project will be documented in this file.
- Staff users can now flag attributes as to be hidden in the storefront. - #4338 by @NyanKiyoshi
- Added flags to attributes: whether they are filterable or not in dashboard and/or storefront. - #4341 by @NyanKiyoshi.
- Attributes can now be flagged as required. - #4340 by @NyanKiyoshi.
+- Added a search position field for the storefront navigation to the attributes. - #4343 by @NyanKiyoshi
## [Unreleased]
diff --git a/saleor/graphql/product/descriptions.py b/saleor/graphql/product/descriptions.py
index c4d2764594..52b04b032f 100644
--- a/saleor/graphql/product/descriptions.py
+++ b/saleor/graphql/product/descriptions.py
@@ -10,6 +10,9 @@ class AttributeDescriptions:
)
FILTERABLE_IN_STOREFRONT = "Whether the attribute can be filtered in storefront"
FILTERABLE_IN_DASHBOARD = "Whether the attribute can be filtered in dashboard"
+ STOREFRONT_SEARCH_POSITION = (
+ "The position of the attribute in the storefront navigation (0 by default)"
+ )
class AttributeValueDescriptions:
diff --git a/saleor/graphql/product/mutations/attributes.py b/saleor/graphql/product/mutations/attributes.py
index 44f6a7834f..2ebe7d8abf 100644
--- a/saleor/graphql/product/mutations/attributes.py
+++ b/saleor/graphql/product/mutations/attributes.py
@@ -45,6 +45,9 @@ class AttributeCreateInput(graphene.InputObjectType):
filterable_in_dashboard = graphene.Boolean(
description=AttributeDescriptions.FILTERABLE_IN_DASHBOARD
)
+ storefront_search_position = graphene.Int(
+ required=False, description=AttributeDescriptions.STOREFRONT_SEARCH_POSITION
+ )
class AttributeUpdateInput(graphene.InputObjectType):
@@ -73,6 +76,9 @@ class AttributeUpdateInput(graphene.InputObjectType):
filterable_in_dashboard = graphene.Boolean(
description=AttributeDescriptions.FILTERABLE_IN_DASHBOARD
)
+ storefront_search_position = graphene.Int(
+ required=False, description=AttributeDescriptions.STOREFRONT_SEARCH_POSITION
+ )
class AttributeAssignInput(graphene.InputObjectType):
diff --git a/saleor/graphql/product/types/attributes.py b/saleor/graphql/product/types/attributes.py
index 9cbf22e5d8..fccbd5e668 100644
--- a/saleor/graphql/product/types/attributes.py
+++ b/saleor/graphql/product/types/attributes.py
@@ -111,6 +111,11 @@ class Attribute(CountableDjangoObjectType, MetadataObjectType):
resolver=resolve_translation,
)
+ storefront_search_position = gql_optimizer.field(
+ graphene.Int(description=AttributeDescriptions.STOREFRONT_SEARCH_POSITION),
+ model_field="storefront_search_position",
+ )
+
class Meta:
description = """
Custom attribute of a product. Attributes can be
@@ -152,6 +157,11 @@ class Attribute(CountableDjangoObjectType, MetadataObjectType):
def resolve_filterable_in_dashboard(root: models.Attribute, *_args):
return root.filterable_in_dashboard
+ @staticmethod
+ @permission_required("product.manage_products")
+ def resolve_storefront_search_position(root: models.Attribute, *_args):
+ return root.storefront_search_position
+
class SelectedAttribute(graphene.ObjectType):
attribute = graphene.Field(
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 0021573a40..48136ee823 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -156,6 +156,7 @@ type Attribute implements Node {
filterableInStorefront: Boolean
filterableInDashboard: Boolean
translation(languageCode: LanguageCodeEnum!): AttributeTranslation
+ storefrontSearchPosition: Int
}
type AttributeAssign {
@@ -209,6 +210,7 @@ input AttributeCreateInput {
visibleInStorefront: Boolean
filterableInStorefront: Boolean
filterableInDashboard: Boolean
+ storefrontSearchPosition: Int
}
type AttributeDelete {
@@ -264,6 +266,7 @@ input AttributeUpdateInput {
visibleInStorefront: Boolean
filterableInStorefront: Boolean
filterableInDashboard: Boolean
+ storefrontSearchPosition: Int
}
type AttributeUpdateMeta {
diff --git a/saleor/product/migrations/0096_migrate_attrs_to_m2m.py b/saleor/product/migrations/0096_migrate_attrs_to_m2m.py
index 0d6ffafd5a..fa0ea9f562 100644
--- a/saleor/product/migrations/0096_migrate_attrs_to_m2m.py
+++ b/saleor/product/migrations/0096_migrate_attrs_to_m2m.py
@@ -171,4 +171,13 @@ class Migration(migrations.Migration):
name="value_required",
field=models.BooleanField(default=False),
),
+ migrations.AlterModelOptions(
+ name="attribute",
+ options={"ordering": ("storefront_search_position", "slug")},
+ ),
+ migrations.AddField(
+ model_name="attribute",
+ name="storefront_search_position",
+ field=models.IntegerField(default=0),
+ ),
]
diff --git a/saleor/product/models.py b/saleor/product/models.py
index f551370cc7..58a0a570ad 100644
--- a/saleor/product/models.py
+++ b/saleor/product/models.py
@@ -529,11 +529,13 @@ class Attribute(ModelWithMetadata):
filterable_in_storefront = models.BooleanField(default=True)
filterable_in_dashboard = models.BooleanField(default=True)
+ storefront_search_position = models.IntegerField(default=0)
+
objects = AttributeQuerySet.as_manager()
translated = TranslationProxy()
class Meta:
- ordering = ("slug",)
+ ordering = ("storefront_search_position", "slug")
def __str__(self):
return self.name
diff --git a/tests/api/test_attributes.py b/tests/api/test_attributes.py
index 9c5f73c6af..ff9f70725b 100644
--- a/tests/api/test_attributes.py
+++ b/tests/api/test_attributes.py
@@ -1194,6 +1194,7 @@ def test_retrieve_product_attributes_input_type(
("filterable_in_dashboard", True),
("visible_in_storefront", True),
("value_required", False),
+ ("storefront_search_position", 0),
),
)
def test_retrieving_the_restricted_attributes_restricted(