Skip to content

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

Add specific products type in voucher

fowczarek3b378646f707
diff --git a/saleor/checkout/utils.py b/saleor/checkout/utils.py
index 5df91cef2c..2253ad92b9 100644
--- a/saleor/checkout/utils.py
+++ b/saleor/checkout/utils.py
@@ -99,6 +99,34 @@ def get_variant_prices_from_lines(lines):
return [line.variant.get_price() for line in lines for item in range(line.quantity)]
+def get_prices_of_discounted_specific_product(lines, voucher):
+ """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.
+ """
+ # If there's no discounted products, collections or categories,
+ # it means that all products are discounted
+ discounted_products = voucher.products.all()
+ discounted_collections = set(voucher.collections.all())
+ discounted_categories = set(voucher.categories.all())
+ if discounted_products or discounted_collections or discounted_categories:
+ lines = (
+ line
+ for line in lines
+ if line.variant
+ and (
+ line.variant.product in discounted_products
+ or set(line.variant.product.collections.all()).intersection(
+ discounted_collections
+ )
+ or line.variant.product.category in discounted_categories
+ )
+ )
+ return get_variant_prices_from_lines(lines)
+
+
def get_prices_of_discounted_products(lines, discounted_products):
"""Get prices of variants belonging to the discounted products."""
# If there's no discounted_products,
@@ -713,9 +741,12 @@ def _get_shipping_voucher_discount_for_checkout(
def _get_products_voucher_discount(order_or_checkout, voucher):
- """Calculate products discount value for a voucher, depending on its type.
- """
- if voucher.type == VoucherType.PRODUCT:
+ """Calculate products discount value for a voucher, depending on its type."""
+ if voucher.type == VoucherType.SPECIFIC_PRODUCT:
+ prices = get_prices_of_discounted_specific_product(
+ order_or_checkout.lines.all(), voucher
+ )
+ elif voucher.type == VoucherType.PRODUCT:
prices = get_prices_of_discounted_products(
order_or_checkout.lines.all(), voucher.products.all()
)
@@ -752,6 +783,7 @@ def get_voucher_discount_for_checkout(voucher, checkout, discounts=None, taxes=N
VoucherType.PRODUCT,
VoucherType.COLLECTION,
VoucherType.CATEGORY,
+ VoucherType.SPECIFIC_PRODUCT,
):
return _get_products_voucher_discount(checkout, voucher)
raise NotImplementedError("Unknown discount type")
diff --git a/saleor/dashboard/order/utils.py b/saleor/dashboard/order/utils.py
index af281e8f99..8763987834 100644
--- a/saleor/dashboard/order/utils.py
+++ b/saleor/dashboard/order/utils.py
@@ -90,6 +90,7 @@ def get_voucher_discount_for_order(order):
VoucherType.PRODUCT,
VoucherType.COLLECTION,
VoucherType.CATEGORY,
+ VoucherType.SPECIFIC_PRODUCT,
):
return _get_products_voucher_discount(order, order.voucher)
raise NotImplementedError("Unknown discount type")
diff --git a/saleor/discount/__init__.py b/saleor/discount/__init__.py
index 474104fc20..8dc87dc67f 100644
--- a/saleor/discount/__init__.py
+++ b/saleor/discount/__init__.py
@@ -21,6 +21,7 @@ class VoucherType:
CATEGORY = "category"
SHIPPING = "shipping"
ENTIRE_ORDER = "entire_order"
+ SPECIFIC_PRODUCT = "specific_product"
CHOICES = [
(ENTIRE_ORDER, pgettext_lazy("Voucher: discount for", "Entire order")),
@@ -34,6 +35,12 @@ class VoucherType:
pgettext_lazy("Voucher: discount for", "Specific categories of products"),
),
(SHIPPING, pgettext_lazy("Voucher: discount for", "Shipping")),
+ (
+ SPECIFIC_PRODUCT,
+ pgettext_lazy(
+ "Voucher: discount for", "Specific products, collections and categories"
+ ),
+ ),
]
diff --git a/saleor/graphql/discount/enums.py b/saleor/graphql/discount/enums.py
index 7cc6be0c4d..a4a23db8eb 100644
--- a/saleor/graphql/discount/enums.py
+++ b/saleor/graphql/discount/enums.py
@@ -14,6 +14,7 @@ class VoucherTypeEnum(graphene.Enum):
CATEGORY = VoucherType.CATEGORY
SHIPPING = VoucherType.SHIPPING
ENTIRE_ORDER = VoucherType.ENTIRE_ORDER
+ SPECIFIC_PRODUCT = VoucherType.SPECIFIC_PRODUCT
class DiscountStatusEnum(graphene.Enum):