Skip to content

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

Add field for minimal quantity of products in voucher

fowczarek6075d068cadb
diff --git a/saleor/dashboard/discount/forms.py b/saleor/dashboard/discount/forms.py
index 053ce47190..52b08344cf 100644
--- a/saleor/dashboard/discount/forms.py
+++ b/saleor/dashboard/discount/forms.py
@@ -93,6 +93,9 @@ class VoucherForm(forms.ModelForm):
"name": pgettext_lazy("Item name", "Name"),
"code": pgettext_lazy("Coupon code", "Code"),
"usage_limit": pgettext_lazy("Usage limit", "Usage limit"),
+ "min_quantity_of_products": pgettext_lazy(
+ "Voucher: discount with", "Minimal quantity of products"
+ ),
"start_date": pgettext_lazy("Voucher date restrictions", "Start date"),
"end_date": pgettext_lazy("Voucher date restrictions", "End date"),
"discount_value_type": pgettext_lazy(
diff --git a/saleor/discount/models.py b/saleor/discount/models.py
index 0a43e51262..0fc10a0e51 100644
--- a/saleor/discount/models.py
+++ b/saleor/discount/models.py
@@ -19,13 +19,15 @@ class NotApplicable(ValueError):
"""Exception raised when a discount is not applicable to a checkout.
The error is raised if the order value is below the minimum required
- price.
+ price or the order quantity is below the minimum quantity of items.
Minimum price will be available as the `min_amount_spent` attribute.
+ Minimum quantity will be available as the `min_quantity_of_products` attribute.
"""
- def __init__(self, msg, min_amount_spent=None):
+ def __init__(self, msg, min_amount_spent=None, min_quantity_of_products=None):
super().__init__(msg)
self.min_amount_spent = min_amount_spent
+ self.min_quantity_of_products = min_quantity_of_products
class VoucherQueryset(models.QuerySet):
@@ -73,6 +75,7 @@ class Voucher(models.Model):
null=True,
blank=True,
)
+ min_quantity_of_products = models.PositiveIntegerField(null=True, blank=True)
products = models.ManyToManyField("product.Product", blank=True)
collections = models.ManyToManyField("product.Collection", blank=True)
categories = models.ManyToManyField("product.Category", blank=True)
@@ -147,6 +150,21 @@ class Voucher(models.Model):
min_amount_spent=min_amount_spent,
)
+ def validate_min_quantity_of_products(self, quantity):
+ min_quantity_of_products = self.min_quantity_of_products
+ if min_quantity_of_products > quantity:
+ msg = pgettext(
+ "Voucher not applicable",
+ (
+ "This offer is only valid for orders with a minimum of "
+ "%(min_quantity_of_products)d quantity."
+ ),
+ )
+ raise NotApplicable(
+ msg % {"min_quantity_of_products": min_quantity_of_products},
+ min_quantity_of_products=min_quantity_of_products,
+ )
+
class SaleQueryset(models.QuerySet):
def active(self, date):
diff --git a/saleor/graphql/discount/mutations.py b/saleor/graphql/discount/mutations.py
index 77f69b85b4..286d55cc8c 100644
--- a/saleor/graphql/discount/mutations.py
+++ b/saleor/graphql/discount/mutations.py
@@ -101,12 +101,15 @@ class VoucherInput(graphene.InputObjectType):
min_amount_spent = Decimal(
description="Min purchase amount required to apply the voucher."
)
+ min_quantity_of_products = graphene.Int(
+ description="Minimal quantity of products required to apply the voucher."
+ )
countries = graphene.List(
graphene.String,
- description="Country codes that can be used with the shipping voucher",
+ description="Country codes that can be used with the shipping voucher.",
)
apply_once_per_order = graphene.Boolean(
- description="Voucher should be applied to the cheapest item or entire order"
+ description="Voucher should be applied to the cheapest item or entire order."
)
usage_limit = graphene.Int(
description="Limit number of times this voucher can be used in total"
diff --git a/saleor/graphql/discount/types.py b/saleor/graphql/discount/types.py
index fe541b2c3d..8d0c3e8bc3 100644
--- a/saleor/graphql/discount/types.py
+++ b/saleor/graphql/discount/types.py
@@ -116,6 +116,7 @@ class Voucher(CountableDjangoObjectType):
"end_date",
"id",
"min_amount_spent",
+ "min_quantity_of_products",
"name",
"start_date",
"type",
diff --git a/tests/api/test_discount.py b/tests/api/test_discount.py
index 2cfd8199a6..ad4ff9ea17 100644
--- a/tests/api/test_discount.py
+++ b/tests/api/test_discount.py
@@ -140,15 +140,15 @@ def test_sale_query(staff_api_client, sale, permission_manage_discounts):
CREATE_VOUCHER_MUTATION = """
mutation voucherCreate(
$type: VoucherTypeEnum, $name: String, $code: String,
- $discountValueType: DiscountValueTypeEnum,
- $discountValue: Decimal, $minAmountSpent: Decimal,
- $startDate: DateTime, $endDate: DateTime, $applyOncePerOrder: Boolean,
- $usageLimit: Int) {
+ $discountValueType: DiscountValueTypeEnum, $usageLimit: Int,
+ $discountValue: Decimal, $minAmountSpent: Decimal, $minQuantityOfProducts: Int,
+ $startDate: DateTime, $endDate: DateTime, $applyOncePerOrder: Boolean) {
voucherCreate(input: {
name: $name, type: $type, code: $code,
discountValueType: $discountValueType,
discountValue: $discountValue,
minAmountSpent: $minAmountSpent,
+ minQuantityOfProducts: $minQuantityOfProducts,
startDate: $startDate, endDate: $endDate,
applyOncePerOrder: $applyOncePerOrder, usageLimit: $usageLimit}) {
errors {
@@ -160,6 +160,7 @@ mutation voucherCreate(
minAmountSpent {
amount
}
+ minQuantityOfProducts
name
code
discountValueType
@@ -183,6 +184,7 @@ def test_create_voucher(staff_api_client, permission_manage_discounts):
"discountValueType": DiscountValueTypeEnum.FIXED.name,
"discountValue": 10.12,
"minAmountSpent": 1.12,
+ "minQuantityOfProducts": 10,
"startDate": start_date.isoformat(),
"endDate": end_date.isoformat(),
"applyOncePerOrder": True,
@@ -317,10 +319,11 @@ def test_update_voucher(staff_api_client, voucher, permission_manage_discounts):
query = """
mutation voucherUpdate($code: String,
$discountValueType: DiscountValueTypeEnum, $id: ID!,
- $applyOncePerOrder: Boolean) {
+ $applyOncePerOrder: Boolean, $minQuantityOfProducts: Int) {
voucherUpdate(id: $id, input: {
code: $code, discountValueType: $discountValueType,
- applyOncePerOrder: $applyOncePerOrder
+ applyOncePerOrder: $applyOncePerOrder,
+ minQuantityOfProducts: $minQuantityOfProducts
}) {
errors {
field
@@ -330,6 +333,7 @@ def test_update_voucher(staff_api_client, voucher, permission_manage_discounts):
code
discountValueType
applyOncePerOrder
+ minQuantityOfProducts
}
}
}
@@ -344,6 +348,7 @@ def test_update_voucher(staff_api_client, voucher, permission_manage_discounts):
"code": "testcode123",
"discountValueType": DiscountValueTypeEnum.PERCENTAGE.name,
"applyOncePerOrder": apply_once_per_order,
+ "minQuantityOfProducts": 10,
}
response = staff_api_client.post_graphql(
@@ -354,6 +359,7 @@ def test_update_voucher(staff_api_client, voucher, permission_manage_discounts):
assert data["code"] == "testcode123"
assert data["discountValueType"] == DiscountValueType.PERCENTAGE.upper()
assert data["applyOncePerOrder"] == apply_once_per_order
+ assert data["minQuantityOfProducts"] == 10
def test_voucher_delete_mutation(