Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix variants filter in discounts
Artur Smętcffe3db3073e
diff --git a/saleor/discount/models.py b/saleor/discount/models.pyindex 7f4d8ed0fe..77d58642e2 100644--- a/saleor/discount/models.py+++ b/saleor/discount/models.py@@ -265,10 +265,9 @@ class Sale(models.Model):return Falsedef modifier_for_variant(self, variant):- check_price = variant.get_price_per_item()discounted_products = [p.pk for p in self.products.all()]discounted_categories = list(self.categories.all())- if discounted_products and variant.pk not in discounted_products:+ if discounted_products and variant.product.pk not in discounted_products:raise NotApplicable('Discount not applicable for this product')if (discounted_categories and notself._product_has_category_discount(diff --git a/saleor/discount/test_discounts.py b/saleor/discount/test_discounts.pyindex ed8198abdb..78e7fc6619 100644--- a/saleor/discount/test_discounts.py+++ b/saleor/discount/test_discounts.py@@ -206,3 +206,20 @@ def test_products_voucher_checkout_discount_not(settings, monkeypatch, prices,checkout = Mock(cart=Mock())discount = voucher.get_discount_for_checkout(checkout)assert discount.amount == Price(expected_value, currency='USD')+++def test_sale_applies_to_correct_products():+ product = Product.objects.create(name='Test Product', price=10, weight=1,+ description='', pk=10)+ variant = ProductVariant.objects.create(product=product, sku='firstvar')+ product2 = Product.objects.create(name='Second product', price=15, weight=1,+ description='')+ sec_variant = ProductVariant.objects.create(product=product2, sku='secvar',+ pk=10)+ 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')+ with pytest.raises(NotApplicable):+ sale.modifier_for_variant(sec_variant)