Skip to content

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

Add docs for discount utils functions

Anna Siwikc69eca4d1a17
diff --git a/saleor/discount/utils.py b/saleor/discount/utils.py
index e0b46c72f9..d6bf42d430 100644
--- a/saleor/discount/utils.py
+++ b/saleor/discount/utils.py
@@ -12,16 +12,42 @@ from . import VoucherApplyToProduct, VoucherType
def increase_voucher_usage(voucher):
+ """Increases voucher uses by 1."""
voucher.used = F('used') + 1
voucher.save(update_fields=['used'])
def decrease_voucher_usage(voucher):
+ """Decreases voucher uses by 1."""
voucher.used = F('used') - 1
voucher.save(update_fields=['used'])
+def is_category_on_sale(category, sale):
+ """Checks if category is descendant of one of categories on sale."""
+ discounted_categories = set(sale.categories.all())
+ return any([
+ category.is_descendant_of(c, include_self=True)
+ for c in discounted_categories])
+
+
+def get_product_discount_on_sale(sale, product):
+ """Returns discount value if product is on sale or raises NotApplicable
+ elsewhere."""
+ discounted_products = {p.pk for p in sale.products.all()}
+ is_product_on_sale = (
+ product.pk in discounted_products or
+ is_category_on_sale(product.category, sale))
+ if is_product_on_sale:
+ return sale.get_discount()
+ raise NotApplicable(
+ pgettext(
+ 'Voucher not applicable',
+ 'Discount not applicable for this product'))
+
+
def get_product_discounts(product, discounts):
+ """Returns discount values for all discounts applicable to a product."""
for discount in discounts:
try:
yield get_product_discount_on_sale(discount, product)
@@ -30,6 +56,7 @@ def get_product_discounts(product, discounts):
def calculate_discounted_price(product, price, discounts):
+ """Returns minimum product's price of all prices with discounts applied."""
if discounts:
discounts = list(
get_product_discounts(product, discounts))
@@ -38,33 +65,15 @@ def calculate_discounted_price(product, price, discounts):
return price
-def is_category_discounted(category, discounted_categories):
- return any([
- category.is_descendant_of(c, include_self=True)
- for c in discounted_categories])
-
-
-def get_product_discount_on_sale(sale, product):
- discounted_products = {p.pk for p in sale.products.all()}
- discounted_categories = set(sale.categories.all())
- is_product_discounted = (
- product.pk in discounted_products or
- is_category_discounted(product.category, discounted_categories))
- if is_product_discounted:
- return sale.get_discount()
- raise NotApplicable(
- pgettext(
- 'Voucher not applicable',
- 'Discount not applicable for this product'))
-
-
def _get_value_voucher_discount_for_checkout(voucher, checkout):
+ """Calculates discount value for a voucher of value type."""
cart_total = checkout.get_subtotal()
voucher.validate_limit(cart_total)
return voucher.get_fixed_discount_for(cart_total)
def _get_shipping_voucher_discount_for_checkout(voucher, checkout):
+ """Calculates discount value for a voucher of shipping type."""
if not checkout.is_shipping_required:
msg = pgettext(
'Voucher not applicable',
@@ -90,6 +99,7 @@ def _get_shipping_voucher_discount_for_checkout(voucher, checkout):
def _get_product_or_category_voucher_discount_for_checkout(voucher, checkout):
+ """Calculates discount value for a voucher of product or category type."""
if voucher.type == VoucherType.PRODUCT:
prices = [
item[1] for item in get_product_variants_and_prices(
@@ -116,6 +126,9 @@ def _get_product_or_category_voucher_discount_for_checkout(voucher, checkout):
def get_voucher_discount_for_checkout(voucher, checkout):
+ """Calculates discount value depending on voucher and discount types.
+
+ Raises NotApplicable if voucher of given type cannot be applied."""
if voucher.type == VoucherType.VALUE:
return _get_value_voucher_discount_for_checkout(voucher, checkout)
if voucher.type == VoucherType.SHIPPING: