Skip to content

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

Fix filters order in storefront

Marcin Gębalafb5983e0064d
diff --git a/saleor/product/filters.py b/saleor/product/filters.py
index 9624eb90f6..f50ae33a4d 100644
--- a/saleor/product/filters.py
+++ b/saleor/product/filters.py
@@ -1,4 +1,5 @@
from collections import OrderedDict
+from itertools import chain
from django.db.models import Q
from django.forms import CheckboxSelectMultiple
@@ -40,10 +41,16 @@ class ProductFilter(SortedFilterSet):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
- self.product_attributes, self.variant_attributes = self._get_attributes()
- self.filters.update(self._get_product_attributes_filters())
- self.filters.update(self._get_product_variants_attributes_filters())
- self.filters = OrderedDict(sorted(self.filters.items()))
+ attributes = self._get_attributes()
+ filters = {}
+ for attribute in attributes:
+ filters[attribute.slug] = JSONBArrayFilter(
+ field_name=f"attributes__from_key_{attribute.pk}",
+ label=attribute.translated.name,
+ widget=CheckboxSelectMultiple,
+ choices=self._get_attribute_choices(attribute),
+ )
+ self.filters = filters
def _get_attributes(self):
q_product_attributes = self._get_product_attributes_lookup()
@@ -60,7 +67,12 @@ class ProductFilter(SortedFilterSet):
.filter(q_variant_attributes)
.distinct()
)
- return product_attributes, variant_attributes
+
+ attributes = chain(product_attributes, variant_attributes)
+ attributes = sorted(
+ attributes, key=lambda attr: attr.storefront_search_position
+ )
+ return attributes
def _get_product_attributes_lookup(self):
raise NotImplementedError()
@@ -68,28 +80,6 @@ class ProductFilter(SortedFilterSet):
def _get_variant_attributes_lookup(self):
raise NotImplementedError()
- def _get_product_attributes_filters(self):
- filters = {}
- for attribute in self.product_attributes:
- filters[attribute.slug] = JSONBArrayFilter(
- field_name=f"attributes__from_key_{attribute.pk}",
- label=attribute.translated.name,
- widget=CheckboxSelectMultiple,
- choices=self._get_attribute_choices(attribute),
- )
- return filters
-
- def _get_product_variants_attributes_filters(self):
- filters = {}
- for attribute in self.variant_attributes:
- filters[attribute.slug] = JSONBArrayFilter(
- field_name=f"variants__attributes__from_key_{attribute.pk}",
- label=attribute.translated.name,
- widget=CheckboxSelectMultiple,
- choices=self._get_attribute_choices(attribute),
- )
- return filters
-
def _get_attribute_choices(self, attribute):
return [
(choice.pk, choice.translated.name) for choice in attribute.values.all()
diff --git a/tests/test_category.py b/tests/test_category.py
index 79ee13940b..d8930756bc 100644
--- a/tests/test_category.py
+++ b/tests/test_category.py
@@ -5,10 +5,10 @@ def test_product_category_filter_filters_from_child_category(
product_type, categories_tree
):
product_filter = ProductCategoryFilter(data={}, category=categories_tree)
- (product_attributes, variant_attributes) = product_filter._get_attributes()
+ attributes = product_filter._get_attributes()
- attribut = product_type.product_attributes.get()
- variant = product_type.variant_attributes.get()
+ product_attr = product_type.product_attributes.get()
+ variant_attr = product_type.variant_attributes.get()
- assert attribut in product_attributes
- assert variant in variant_attributes
+ assert product_attr in attributes
+ assert variant_attr in attributes