Skip to content

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

Show filters relevant to the current category

Marcin Gębalaa25b71e5d8e9
diff --git a/saleor/graphql/api.py b/saleor/graphql/api.py
index 548ae14351..4c7bc09ead 100644
--- a/saleor/graphql/api.py
+++ b/saleor/graphql/api.py
@@ -193,15 +193,30 @@ class PriceType(graphene.ObjectType):
class Viewer(graphene.ObjectType):
category = graphene.Field(
- CategoryType, pk=graphene.Argument(graphene.Int, required=True))
- attributes = graphene.List(ProductAttributeType)
+ CategoryType,
+ pk=graphene.Argument(graphene.Int, required=True))
+ attributes = graphene.List(
+ ProductAttributeType,
+ category_pk=graphene.Argument(graphene.Int, required=False))
debug = graphene.Field(DjangoDebug, name='__debug')
def resolve_category(self, args, context, info):
return get_object_or_none(Category, pk=args.get('pk'))
def resolve_attributes(self, args, context, info):
- return ProductAttribute.objects.prefetch_related('values').all()
+ category_pk = args.get('category_pk')
+ queryset = ProductAttribute.objects.prefetch_related('values')
+ if category_pk:
+ # Get attributes that are used with product classes
+ # within the given category.
+ product_classes = set(
+ [obj[0] for obj in Product.objects.filter(
+ categories__in=[category_pk]).values_list(
+ 'product_class_id')])
+ queryset = queryset.filter(
+ Q(products_class__in=product_classes) |
+ Q(product_variants_class__in=product_classes))
+ return queryset
class Query(graphene.ObjectType):
diff --git a/saleor/static/js/category.js b/saleor/static/js/category.js
index 4449b64e66..0a884a7fe6 100644
--- a/saleor/static/js/category.js
+++ b/saleor/static/js/category.js
@@ -40,7 +40,7 @@ const RelayApp = Relay.createContainer(App, {
category(pk: $categoryId) {
${CategoryPage.getFragment('category')}
}
- attributes {
+ attributes(categoryPk: $categoryId) {
${ProductFilters.getFragment('attributes')}
},
__debug {
diff --git a/saleor/static/js/components/CategoryFilter.js b/saleor/static/js/components/CategoryFilter.js
index 0070df30f3..7449fd8049 100644
--- a/saleor/static/js/components/CategoryFilter.js
+++ b/saleor/static/js/components/CategoryFilter.js
@@ -1,5 +1,4 @@
import React, { Component, PropTypes } from 'react';
-import AttributeInput from './AttributeInput';
export default class CategoryFilter extends Component {