Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Calculate discount for product price
Miracd3732af7d21
diff --git a/saleor/discount/models.py b/saleor/discount/models.pyindex b697ab1ee0..aa01d1a4ff 100644--- a/saleor/discount/models.py+++ b/saleor/discount/models.py@@ -285,17 +285,17 @@ class Sale(models.Model):return Truereturn False- def modifier_for_variant(self, variant):+ def modifier_for_product(self, product):discounted_products = [p.pk for p in self.products.all()]discounted_categories = list(self.categories.all())- if discounted_products and variant.product.pk not in discounted_products:+ if discounted_products and product.pk not in discounted_products:raise NotApplicable(pgettext('Voucher not applicable','Discount not applicable for this product'))if (discounted_categories and not- self._product_has_category_discount(- variant.product, discounted_categories)):+ self._product_has_category_discount(+ product, discounted_categories)):raise NotApplicable(pgettext('Voucher not applicable',@@ -303,9 +303,9 @@ class Sale(models.Model):return self.get_discount()-def get_variant_discounts(variant, discounts, **kwargs):+def get_product_discounts(product, discounts, **kwargs):for discount in discounts:try:- yield discount.modifier_for_variant(variant, **kwargs)+ yield discount.modifier_for_product(product, **kwargs)except NotApplicable:passdiff --git a/saleor/product/models/base.py b/saleor/product/models/base.pyindex 56f617f0ef..2fb00157fc 100644--- a/saleor/product/models/base.py+++ b/saleor/product/models/base.py@@ -20,7 +20,7 @@ from prices import PriceRangefrom satchless.item import InsufficientStock, Item, ItemRangefrom text_unidecode import unidecode-from ...discount.models import get_variant_discounts+from ...discount.models import get_product_discountsfrom .utils import get_attributes_display_mapfrom ...search import index@@ -202,11 +202,18 @@ class Product(models.Model, ItemRange, index.Indexed):def set_attribute(self, pk, value_pk):self.attributes[smart_text(pk)] = smart_text(value_pk)- def get_price_range(self, **kwargs):+ def get_price_range(self, discounts=None, **kwargs):if not self.variants.exists():- return PriceRange(self.price, self.price)+ price = self.price+ if discounts:+ discounts = list(+ get_product_discounts(self, discounts, **kwargs))+ if discounts:+ price = min(price | discount for discount in discounts)+ return PriceRange(price, price)else:- return super(Product, self).get_price_range(**kwargs)+ return super(Product, self).get_price_range(+ discounts=discounts, **kwargs)@python_2_unicode_compatible@@ -249,7 +256,7 @@ class ProductVariant(models.Model, Item):price = self.price_override or self.product.priceif discounts:discounts = list(- get_variant_discounts(self, discounts, **kwargs))+ get_product_discounts(self.product, discounts, **kwargs))if discounts:price = min(price | discount for discount in discounts)return pricediff --git a/tests/test_discounts.py b/tests/test_discounts.pyindex 0282c25741..3ae3fdb3e8 100644--- a/tests/test_discounts.py+++ b/tests/test_discounts.py@@ -242,7 +242,7 @@ def test_sale_applies_to_correct_products(product_class):sale = Sale.objects.create(name='Test sale', value=5, type=Sale.FIXED)sale.products.add(product)assert product2 not in sale.products.all()- assert sale.modifier_for_variant(variant).amount == Price(net=5,- currency='USD')+ assert sale.modifier_for_product(variant.product).amount == Price(+ net=5, currency='USD')with pytest.raises(NotApplicable):- sale.modifier_for_variant(sec_variant)+ sale.modifier_for_product(sec_variant.product)