Skip to content

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

Fix filtering products

Krzysztof Świątekab72181fc520
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py
index c6dc2d1282..fccea17270 100644
--- a/saleor/graphql/product/filters.py
+++ b/saleor/graphql/product/filters.py
@@ -51,7 +51,7 @@ def _clean_product_attributes_filter_input(filter_value) -> T_PRODUCT_FILTER_QUE
if attr_name not in attributes_map:
raise ValueError("Unknown attribute name: %r" % (attr_name,))
attr_pk = attributes_map[attr_name]
- attr_val_pk = values_map[attr_name].get(val_slug, val_slug)
+ attr_val_pk = values_map[attr_name].get(val_slug)
queries[attr_pk].append(attr_val_pk)
return queries
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 2cfc84c5c0..132a9eaab0 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -568,6 +568,33 @@ def test_filter_products_by_attributes(user_api_client, product):
assert products[0]["node"]["name"] == product.name
+def test_filter_products_by_wrong_attributes(user_api_client, product):
+ product_attr = product.product_type.product_attributes.get(slug="color")
+ attr_value = (
+ product.product_type.variant_attributes.get(slug="size").values.first().id
+ )
+ filter_by = "%s:%s" % (product_attr.slug, attr_value)
+ query = """
+ query {
+ products(attributes: ["%(filter_by)s"], first: 1) {
+ edges {
+ node {
+ name
+ }
+ }
+ }
+ }
+ """ % {
+ "filter_by": filter_by
+ }
+
+ response = user_api_client.post_graphql(query)
+ content = get_graphql_content(response)
+ products = content["data"]["products"]["edges"]
+
+ assert products == []
+
+
def test_filter_products_by_categories(user_api_client, categories_tree, product):
category = categories_tree.children.first()
product.category = category