Skip to content

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

Add filter by sku to products variant query

fowczrekd926883be42f
diff --git a/saleor/graphql/product/filters.py b/saleor/graphql/product/filters.py
index ce952aecb3..ed64117ead 100644
--- a/saleor/graphql/product/filters.py
+++ b/saleor/graphql/product/filters.py
@@ -265,6 +265,12 @@ def filter_warehouses(qs, _, value):
return qs
+def filter_sku_list(qs, _, value):
+ if value:
+ return qs.filter(sku__in=value)
+ return qs
+
+
def filter_quantity(qs, quantity_value, warehouses=None):
"""Filter products queryset by product variants quantity.
@@ -340,6 +346,7 @@ class ProductVariantFilter(django_filters.FilterSet):
search = django_filters.CharFilter(
method=filter_fields_containing_value("name", "product__name", "sku")
)
+ sku = ListObjectTypeFilter(input_class=graphene.String, method=filter_sku_list)
class Meta:
model = ProductVariant
diff --git a/saleor/graphql/product/tests/test_variant_with_filtering.py b/saleor/graphql/product/tests/test_variant_with_filtering.py
index 1851691981..43545f2143 100644
--- a/saleor/graphql/product/tests/test_variant_with_filtering.py
+++ b/saleor/graphql/product/tests/test_variant_with_filtering.py
@@ -81,6 +81,11 @@ def products_for_variant_filtering(product_type, category):
({"search": "XXL"}, ["PP2-V1"]),
({"search": "PP2-V1"}, ["PP2-V1"]),
({"search": "P1"}, ["P1-V1", "P1-V2", "PP1-V1"]),
+ ({"search": ["invalid"]}, []),
+ ({"sku": ["P1"]}, []),
+ ({"sku": ["P1-V1", "P1-V2", "PP1-V1"]}, ["P1-V1", "P1-V2", "PP1-V1"]),
+ ({"sku": ["PP1-V1", "PP2-V1"]}, ["PP1-V1", "PP2-V1"]),
+ ({"sku": ["invalid"]}, []),
],
)
def test_products_pagination_with_filtering(
diff --git a/saleor/graphql/schema.graphql b/saleor/graphql/schema.graphql
index 0d0f7dba44..3d4231d6f7 100644
--- a/saleor/graphql/schema.graphql
+++ b/saleor/graphql/schema.graphql
@@ -4169,6 +4169,7 @@ type ProductVariantDelete {
input ProductVariantFilterInput {
search: String
+ sku: [String]
}
input ProductVariantInput {