Merge branch 'only-one-coupon-per-transaction' into 'master'

No coupon stacking, only one per transaction

See merge request engineering/make-post-sell/make_post_sell!27
This commit is contained in:
Russell Ballestrini 2024-06-25 23:29:57 +00:00
commit 2f4bfaca92
2 changed files with 18 additions and 10 deletions

View file

@ -394,36 +394,39 @@ class Cart(RBase, Base):
return timestamp_to_ago_string(self.created_timestamp)
def validate_attached_coupons(self):
"""This routine makes sure all attached coupon have terms met."""
"""This routine makes sure all attached coupons have terms met."""
error_messages = []
if self.coupons:
if len(self.coupons) > 1:
error_messages.append(
"We don't support coupon stacking. Please choose one coupon."
)
for coupon in self.coupons:
# test if the shop's cart total qualifies for this coupon.
if coupon.is_not_valid:
error_messages.append(
"Sorry that coupon is not valid (expired or disabled)."
f"Sorry, the coupon '{coupon.code}' is not valid (expired or disabled)."
)
elif (
self.shop_totals_in_cents[coupon.shop_uuid_str]
< (coupon.cart_qualifier or 0)
if self.shop_totals_in_cents.get(coupon.shop_uuid_str, 0) <= (
coupon.cart_qualifier or 0
):
error_messages.append(
"Please review coupon terms: shop total not met."
f"Please review the terms for coupon '{coupon.code}': shop total not met."
)
elif (
if (
coupon.max_redemptions
and coupon.redemptions.count() >= coupon.max_redemptions
):
error_messages.append(
"That coupon was used too many times. Please remove it."
f"The coupon '{coupon.code}' was used too many times. Please remove it."
)
elif (
if (
coupon.max_redemptions_per_user
and coupon.redemptions.filter_by(user_id=self.user_id).count()
>= coupon.max_redemptions_per_user
):
error_messages.append(
"You have already used this coupon. Please remove it."
f"You have already used the coupon '{coupon.code}'. Please remove it."
)
return error_messages

View file

@ -38,6 +38,11 @@ def coupons(request):
@view_config(route_name="coupon1", renderer="coupon.j2")
@view_config(route_name="coupon2", renderer="coupon.j2")
def coupon(request):
if not request.is_saas_domain and request.domain != request.shop.domain_name:
# The coupond uuid in the URI matchdict is mismatched with request shop.
request.session.flash(("Refusing to display another shop's coupons.", "error"))
return HTTPFound(get_referer_or_home(request))
coupon = get_coupon_from_matchdict(request)
return {"coupon": coupon}