Saleor Core: the high performance, composable, headless commerce API.
Python cartcheckoutcommercecomposablee-commerceecommercegraphqlheadlessheadless-commercemultichannelomsorder-managementpaymentspimpythonshopshopping-cartstore
Fix Specyfic product voucher in draft order
fowczrekec82cae9678b
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.pyindex ce8c68839a..b012815040 100644--- a/saleor/checkout/utils.py+++ b/saleor/checkout/utils.py@@ -27,6 +27,7 @@ from ..discount.models import NotApplicable, Voucherfrom ..discount.utils import (add_voucher_usage_by_customer,decrease_voucher_usage,+ get_discounted_lines,get_products_voucher_discount,increase_voucher_usage,remove_voucher_usage_by_customer,@@ -258,28 +259,8 @@ def get_prices_of_discounted_specific_product(Product must be assigned directly to the discounted category, assigningproduct to child category won't work."""- discounted_products = voucher.products.all()- discounted_categories = set(voucher.categories.all())- discounted_collections = set(voucher.collections.all())-line_prices = []- discounted_lines = []- if discounted_products or discounted_collections or discounted_categories:- for line in lines:- line_product = line.variant.product- line_category = line.variant.product.category- line_collections = set(line.variant.product.collections.all())- if line.variant and (- line_product in discounted_products- or line_category in discounted_categories- or line_collections.intersection(discounted_collections)- ):- discounted_lines.append(line)- else:- # If there's no discounted products, collections or categories,- # it means that all products are discounted- discounted_lines.extend(list(lines))-+ discounted_lines = get_discounted_lines(lines, voucher)for line in discounted_lines:line_total = calculations.checkout_line_total(line=line, discounts=discounts or []diff --git a/saleor/discount/utils.py b/saleor/discount/utils.pyindex 56f021aaa1..217b81b840 100644--- a/saleor/discount/utils.py+++ b/saleor/discount/utils.py@@ -1,6 +1,6 @@import datetimefrom collections import defaultdict-from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Set, Union+from typing import TYPE_CHECKING, Dict, Iterable, List, Optional, Setfrom django.db.models import Ffrom django.utils import timezone@@ -96,6 +96,30 @@ def calculate_discounted_price(return price+def get_discounted_lines(lines, voucher):+ discounted_products = voucher.products.all()+ discounted_categories = set(voucher.categories.all())+ discounted_collections = set(voucher.collections.all())++ discounted_lines = []+ if discounted_products or discounted_collections or discounted_categories:+ for line in lines:+ line_product = line.variant.product+ line_category = line.variant.product.category+ line_collections = set(line.variant.product.collections.all())+ if line.variant and (+ line_product in discounted_products+ or line_category in discounted_categories+ or line_collections.intersection(discounted_collections)+ ):+ discounted_lines.append(line)+ else:+ # If there's no discounted products, collections or categories,+ # it means that all products are discounted+ discounted_lines.extend(list(lines))+ return discounted_lines++def validate_voucher_for_checkout(voucher: "Voucher",checkout: "Checkout",diff --git a/saleor/order/tests/test_order.py b/saleor/order/tests/test_order.pyindex e87f2c41d6..96482039ca 100644--- a/saleor/order/tests/test_order.py+++ b/saleor/order/tests/test_order.py@@ -579,6 +579,39 @@ def test_shipping_voucher_checkout_discount_not_applicable_returns_zero(get_voucher_discount_for_order(order)+ "discount_value, discount_type, apply_once_per_order, discount_amount",+ [+ (5, DiscountValueType.FIXED, True, "5"),+ (5, DiscountValueType.FIXED, False, "25"),+ (10000, DiscountValueType.FIXED, True, "12.3"),+ (10000, DiscountValueType.FIXED, False, "86.1"),+ (10, DiscountValueType.PERCENTAGE, True, "1.23"),+ (10, DiscountValueType.PERCENTAGE, False, "8.61"),+ ],+)+def test_get_discount_for_order_specific_products_voucher(+ order_with_lines,+ discount_value,+ discount_type,+ apply_once_per_order,+ discount_amount,+):+ voucher = Voucher.objects.create(+ code="unique",+ type=VoucherType.SPECIFIC_PRODUCT,+ discount_value_type=discount_type,+ discount_value=discount_value,+ apply_once_per_order=apply_once_per_order,+ )+ voucher.products.add(order_with_lines.lines.first().variant.product)+ voucher.products.add(order_with_lines.lines.last().variant.product)+ order_with_lines.voucher = voucher+ order_with_lines.save()+ discount = get_voucher_discount_for_order(order_with_lines)+ assert discount == Money(discount_amount, "USD")++def test_product_voucher_checkout_discount_raises_not_applicable(order_with_lines, product_with_images):diff --git a/saleor/order/utils.py b/saleor/order/utils.pyindex 5e1ff74de0..9fe52fa42a 100644--- a/saleor/order/utils.py+++ b/saleor/order/utils.py@@ -1,4 +1,5 @@from functools import wraps+from typing import Iterable, Listfrom django.conf import settingsfrom django.db import transaction@@ -9,7 +10,11 @@ from ..account.models import Userfrom ..core.taxes import zero_moneyfrom ..core.weight import zero_weightfrom ..discount.models import NotApplicable, Voucher, VoucherType-from ..discount.utils import get_products_voucher_discount, validate_voucher_in_order+from ..discount.utils import (+ get_discounted_lines,+ get_products_voucher_discount,+ validate_voucher_in_order,+)from ..order import OrderStatusfrom ..order.models import Order, OrderLinefrom ..plugins.manager import get_plugins_manager@@ -306,13 +311,34 @@ def get_valid_shipping_methods_for_order(order: Order):)-def get_products_voucher_discount_for_order(voucher: Voucher) -> Money:+def get_prices_of_discounted_specific_product(+ lines: Iterable[OrderLine], voucher: Voucher,+) -> List[Money]:+ """Get prices of variants belonging to the discounted specific products.++ Specific products are products, collections and categories.+ Product must be assigned directly to the discounted category, assigning+ product to child category won't work.+ """+ line_prices = []+ discounted_lines = get_discounted_lines(lines, voucher)++ for line in discounted_lines:+ line_prices.extend([line.unit_price_gross] * line.quantity)++ return line_prices+++def get_products_voucher_discount_for_order(order: Order) -> Money:"""Calculate products discount value for a voucher, depending on its type."""prices = None+ voucher = order.voucher+ if voucher and voucher.type == VoucherType.SPECIFIC_PRODUCT:+ prices = get_prices_of_discounted_specific_product(order.lines.all(), voucher)if not prices:msg = "This offer is only valid for selected items."raise NotApplicable(msg)- return get_products_voucher_discount(voucher, prices)+ return get_products_voucher_discount(voucher, prices) # type: ignoredef get_voucher_discount_for_order(order: Order) -> Money:@@ -329,7 +355,7 @@ def get_voucher_discount_for_order(order: Order) -> Money:if order.voucher.type == VoucherType.SHIPPING:return order.voucher.get_discount_amount_for(order.shipping_price)if order.voucher.type == VoucherType.SPECIFIC_PRODUCT:- return get_products_voucher_discount_for_order(order.voucher)+ return get_products_voucher_discount_for_order(order)raise NotImplementedError("Unknown discount type")