Skip to content

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

Create helper function for calculating discounted price

Mira125341bd0abb
diff --git a/saleor/discount/models.py b/saleor/discount/models.py
index aa01d1a4ff..50ca66b113 100644
--- a/saleor/discount/models.py
+++ b/saleor/discount/models.py
@@ -309,3 +309,12 @@ def get_product_discounts(product, discounts, **kwargs):
yield discount.modifier_for_product(product, **kwargs)
except NotApplicable:
pass
+
+
+def calculate_discounted_price(product, price, discounts, **kwargs):
+ if discounts:
+ discounts = list(
+ get_product_discounts(product, discounts, **kwargs))
+ if discounts:
+ price = min(price | discount for discount in discounts)
+ return price
diff --git a/saleor/product/models/base.py b/saleor/product/models/base.py
index 2fb00157fc..769777d927 100644
--- a/saleor/product/models/base.py
+++ b/saleor/product/models/base.py
@@ -20,7 +20,7 @@ from prices import PriceRange
from satchless.item import InsufficientStock, Item, ItemRange
from text_unidecode import unidecode
-from ...discount.models import get_product_discounts
+from ...discount.models import calculate_discounted_price
from .utils import get_attributes_display_map
from ...search import index
@@ -204,12 +204,8 @@ class Product(models.Model, ItemRange, index.Indexed):
def get_price_range(self, discounts=None, **kwargs):
if not self.variants.exists():
- price = self.price
- if discounts:
- discounts = list(
- get_product_discounts(self, discounts, **kwargs))
- if discounts:
- price = min(price | discount for discount in discounts)
+ price = calculate_discounted_price(self, self.price, discounts,
+ **kwargs)
return PriceRange(price, price)
else:
return super(Product, self).get_price_range(
@@ -254,11 +250,8 @@ class ProductVariant(models.Model, Item):
def get_price_per_item(self, discounts=None, **kwargs):
price = self.price_override or self.product.price
- if discounts:
- discounts = list(
- get_product_discounts(self.product, discounts, **kwargs))
- if discounts:
- price = min(price | discount for discount in discounts)
+ price = calculate_discounted_price(self.product, price, discounts,
+ **kwargs)
return price
def get_absolute_url(self):