Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Made sorting menu dynamic
dominik-zeglenfa59bba3ec92
diff --git a/saleor/product/filters.py b/saleor/product/filters.pyindex df9a5eb671..3c29bd73e6 100644--- a/saleor/product/filters.py+++ b/saleor/product/filters.py@@ -10,8 +10,8 @@ from django_prices.models import PriceFieldfrom .models import Product, ProductAttribute-SORT_BY_FIELDS = (('price', pgettext_lazy('Sort by filter', 'price')),- ('name', pgettext_lazy('Sort by filter', 'name')))+SORT_BY_FIELDS = (('name', pgettext_lazy('Sort by filter', 'name')),+ ('price', pgettext_lazy('Sort by filter', 'price')))class ProductFilter(FilterSet):diff --git a/saleor/product/templatetags/sort_by.py b/saleor/product/templatetags/sort_by.pyindex 490e09fe96..8b590a3ee9 100644--- a/saleor/product/templatetags/sort_by.py+++ b/saleor/product/templatetags/sort_by.py@@ -5,6 +5,7 @@ register = template.Library()@register.inclusion_tag('category/_sort_by.html', takes_context=True)def sort_by(context, attribute):- context['ascending_attribute'] = attribute- context['descending_attribute'] = '-' + str(attribute)+ context['label'] = attribute['label']+ context['ascending_attribute'] = attribute['value']+ context['descending_attribute'] = '-' + str(attribute['value'])return contextdiff --git a/saleor/product/templatetags/sort_menu.py b/saleor/product/templatetags/sort_menu.pynew file mode 100644index 0000000000..0d0f1ef780--- /dev/null+++ b/saleor/product/templatetags/sort_menu.py@@ -0,0 +1,17 @@+from django import template++register = template.Library()++[email protected]_tag('category/_sort_menu.html', takes_context=True)+def sort_menu(context, attribute):+ t = []+ for attr in [a[0] for a in attribute]:+ t.append(attr)+ t.append('-' + str(attr))+ choices = [a[1] for a in attribute]+ context['sort_by_choices'] = [{'label': a[1],+ 'value': a[0]} for a in attribute]+ context['choice'] = choices[int(t.index(context['request'].GET.get('sort_by', 'name')) / 2)]+ context['arrow_up'] = (context['request'].GET.get('sort_by', 'name')[0] != '-')+ return contextdiff --git a/saleor/product/views.py b/saleor/product/views.pyindex 6768be0d74..c0c9f8de53 100644--- a/saleor/product/views.py+++ b/saleor/product/views.py@@ -129,6 +129,6 @@ def category_index(request, path, category_id):ctx = {'category': category, 'filter': product_filter,'products': products_and_availability,'products_paginated': products_paginated,- 'sort_by_choices': [choice[1] for choice in SORT_BY_FIELDS],- 'show_pagination': True if len(products) > PAGINATE_BY else False}+ 'sort_by_choices': SORT_BY_FIELDS,+ 'show_pagination': len(products) > PAGINATE_BY }return TemplateResponse(request, 'category/index.html', ctx)diff --git a/templates/category/_sort_by.html b/templates/category/_sort_by.htmlindex eedaf59aff..e0e4cf88b7 100644--- a/templates/category/_sort_by.html+++ b/templates/category/_sort_by.html@@ -5,7 +5,7 @@<li><div class="row"><div class="col-6">- Sort by: <strong>{{ ascending_attribute }}</strong>+ Sort by: <strong>{{ label }}</strong></div><div class="col-6"><div>diff --git a/templates/category/_sort_menu.html b/templates/category/_sort_menu.htmlnew file mode 100644index 0000000000..185aa882c3--- /dev/null+++ b/templates/category/_sort_menu.html@@ -0,0 +1,30 @@+{% load sort_by from sort_by %}+{% load staticfiles %}+{% load i18n %}+{% load shop %}++<div class="sort-by">+ <div class="click-area d-none"></div>+ <button class="btn btn-link">+ <div>+ <span>+ Sort by: + <strong>+ {{ choice }}+ </strong>+ </span>+ <div class="sort-order-icon">+ {% if arrow_up %}+ <svg data-src="{% static "assets/arrow_up.svg" %}">+ {% else %}+ <svg data-src="{% static "assets/arrow_down.svg" %}">+ {% endif %}+ </div>+ </div>+ </button>+ <ul class="sort-list d-none">+ {% for choice in sort_by_choices %}+ {% sort_by choice %}+ {% endfor %}+ </ul>+</div>diff --git a/templates/category/index.html b/templates/category/index.htmlindex 8313f2bacc..aa511f1e0e 100644--- a/templates/category/index.html+++ b/templates/category/index.html@@ -3,7 +3,7 @@{% load i18n %}{% load shop %}{% load staticfiles %}-{% load sort_by from sort_by %}+{% load sort_menu from sort_menu %}{% load render_bundle from webpack_loader %}{% load prices_i18n %}@@ -45,35 +45,7 @@<span class="filters-menu__label d-sm-none">Filters</span></div><div class="col-6 col-md-10 col-lg-6">- <div class="sort-by">- <div class="click-area d-none"></div>- <button class="btn btn-link">- <div>- <span>- Sort by: - <strong>- {% if request.GET.sort_by in sort_by_choices %}- {% trans "name" context "Product name" %}- {% elif request.GET.sort_by == 'price' or request.GET.sort_by == '-price' %}- {% trans "price" context "Product price" %}- {% endif %}- </strong>- </span>- <div class="sort-order-icon">- {% if request.GET.sort_by == '-name' or request.GET.sort_by == '-price' %}- <svg data-src="{% static "assets/arrow_down.svg" %}">- {% else %}- <svg data-src="{% static "assets/arrow_up.svg" %}">- {% endif %}- </div>- </div>- </button>- <ul class="sort-list d-none">- {% for choice in sort_by_choices %}- {% sort_by choice %}- {% endfor %}- </ul>- </div>+ {% sort_menu sort_by_choices %}</div></div></div>