Skip to content

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

Add serch by sku on products query

fowczarek7842a952f8c6
diff --git a/saleor/search/backends/postgresql_storefront.py b/saleor/search/backends/postgresql_storefront.py
index f60db717f2..759e2aa4cc 100644
--- a/saleor/search/backends/postgresql_storefront.py
+++ b/saleor/search/backends/postgresql_storefront.py
@@ -18,7 +18,8 @@ def search(phrase):
name_sim = TrigramSimilarity("name", phrase)
published = Q(is_published=True)
ft_in_description = Q(description__search=phrase)
+ ft_by_sku = Q(variants__sku__search=phrase)
name_similar = Q(name_sim__gt=0.2)
return Product.objects.annotate(name_sim=name_sim).filter(
- (ft_in_description | name_similar) & published
+ (ft_in_description | name_similar | ft_by_sku) & published
)
diff --git a/tests/api/test_product.py b/tests/api/test_product.py
index 1c85ba7557..0f69c568c8 100644
--- a/tests/api/test_product.py
+++ b/tests/api/test_product.py
@@ -449,6 +449,29 @@ def test_products_query_with_filter(
assert products[0]["node"]["name"] == second_product.name
[email protected]("is_published", [(True), (False)])
+def test_products_query_with_filter_search_by_sku(
+ is_published,
+ query_products_with_filter,
+ staff_api_client,
+ product_with_two_variants,
+ product_with_default_variant,
+ permission_manage_products,
+):
+ product_with_default_variant.is_published = is_published
+ product_with_default_variant.save(update_fields=["is_published"])
+ variables = {"filter": {"search": "1234"}}
+ staff_api_client.user.user_permissions.add(permission_manage_products)
+ response = staff_api_client.post_graphql(query_products_with_filter, variables)
+ content = get_graphql_content(response)
+ product_id = graphene.Node.to_global_id("Product", product_with_default_variant.id)
+ products = content["data"]["products"]["edges"]
+
+ assert len(products) == 1
+ assert products[0]["node"]["id"] == product_id
+ assert products[0]["node"]["name"] == product_with_default_variant.name
+
+
def test_product_query_search(user_api_client, product_type, category):
blue_product = Product.objects.create(
name="Blue Paint",