Skip to content

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

Move logical condition to backend

Anna Siwik725a127483e1
diff --git a/saleor/graphql/product/types.py b/saleor/graphql/product/types.py
index 20c5a5e5b7..fcc47c950a 100644
--- a/saleor/graphql/product/types.py
+++ b/saleor/graphql/product/types.py
@@ -28,6 +28,7 @@ def get_ancestors_from_cache(category, context):
class ProductAvailabilityType(graphene.ObjectType):
available = graphene.Boolean()
+ on_sale = graphene.Boolean()
discount = graphene.Field(lambda: PriceType)
discount_local_currency = graphene.Field(lambda: PriceType)
price_range = graphene.Field(lambda: PriceRangeType)
diff --git a/saleor/product/utils.py b/saleor/product/utils.py
index 1319fe8510..0e0c1f12a6 100644
--- a/saleor/product/utils.py
+++ b/saleor/product/utils.py
@@ -52,8 +52,8 @@ def products_with_availability(products, discounts, local_currency):
ProductAvailability = namedtuple(
'ProductAvailability', (
- 'available', 'price_range', 'price_range_undiscounted', 'discount',
- 'price_range_local_currency', 'discount_local_currency'))
+ 'available', 'on_sale', 'price_range', 'price_range_undiscounted',
+ 'discount', 'price_range_local_currency', 'discount_local_currency'))
def get_availability(product, discounts=None, local_currency=None):
@@ -82,9 +82,13 @@ def get_availability(product, discounts=None, local_currency=None):
discount_local_currency = None
is_available = product.is_in_stock() and product.is_available()
+ is_on_sale = (
+ product.is_available() and discount is not None
+ and undiscounted.min_price != price_range.min_price)
return ProductAvailability(
available=is_available,
+ on_sale=is_on_sale,
price_range=price_range,
price_range_undiscounted=undiscounted,
discount=discount,
diff --git a/templates/_items.html b/templates/_items.html
index e10ccd48b5..00179ca6ce 100644
--- a/templates/_items.html
+++ b/templates/_items.html
@@ -22,16 +22,14 @@
{% else %}
 
{% endif %}
- {% if availability.available and availability.discount %}
- {% if availability.price_range_undiscounted.min_price != availability.price_range.min_price %}
- <div class="product-list__sale">
- <svg data-src="{% static "images/sale-bg.svg" %}" />
- <span class="product-list__sale__text">
- {% comment %}Translators: Layout may break if character length is different than four.{% endcomment %}
- {% trans "Sale" context "Sale (discount) label for item in product list" %}
- </span>
- </div>
- {% endif %}
+ {% if availability.on_sale %}
+ <div class="product-list__sale">
+ <svg data-src="{% static "images/sale-bg.svg" %}" />
+ <span class="product-list__sale__text">
+ {% comment %}Translators: Layout may break if character length is different than four.{% endcomment %}
+ {% trans "Sale" context "Sale (discount) label for item in product list" %}
+ </span>
+ </div>
{% endif %}
</div>
</div>