From 715347cb354f3f5f05cb51f7d3d4a98dd0e6ee2f Mon Sep 17 00:00:00 2001 From: Mira Weller Date: Mon, 17 Jun 2024 14:00:31 +0200 Subject: [PATCH] filter non-buyable items from list --- src/pretix/base/models/items.py | 1 - src/pretix/presale/checkoutflow.py | 19 ++++++++++++------- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/pretix/base/models/items.py b/src/pretix/base/models/items.py index 489d9da1fc..4aa82b6936 100644 --- a/src/pretix/base/models/items.py +++ b/src/pretix/base/models/items.py @@ -161,7 +161,6 @@ class ItemCategory(LoggedModel): if self.cross_selling_condition == 'always': return self.items.all(), {} if self.cross_selling_condition == 'products': - # TODO set max_count for products with max_per_order match = set(match.pk for match in self.cross_selling_match_products.only('pk')) # TODO prefetch this return (self.items.all(), {}) if any(pos.item.pk in match for pos in cart) else (None, {}) if self.cross_selling_condition == 'discounts': diff --git a/src/pretix/presale/checkoutflow.py b/src/pretix/presale/checkoutflow.py index c6cb3e4cf3..4977f1d5ba 100644 --- a/src/pretix/presale/checkoutflow.py +++ b/src/pretix/presale/checkoutflow.py @@ -686,18 +686,15 @@ class AddOnsStep(CartMixin, AsyncAction, TemplateFlowStep): if self.request.customer else None ), ) - + new_items = list() for item in items: + max_count = inf if item.pk in discount_info: (max_count, discount_rule) = discount_info[item.pk] - # set item.order_max for benefit_only_apply_to_cheapest_n_matches discounted items + # only benefit_only_apply_to_cheapest_n_matches discounted items have a max_count, all others get 'inf' if not max_count: max_count = inf - item.order_max = min( - item.order_max - sum(1 for pos in self.positions if pos.item_id == item.pk), - max_count - ) # calculate discounted price if discount_rule: @@ -708,7 +705,15 @@ class AddOnsStep(CartMixin, AsyncAction, TemplateFlowStep): ) item.display_price = new_price - return items + # reduce order_max by number of items already in cart (prevent recommending a product the user can't add anyway) + item.order_max = min( + item.order_max - sum(1 for pos in self.positions if pos.item_id == item.pk), + max_count + ) + if item.order_max > 0: + new_items.append(item) + + return new_items def get_context_data(self, **kwargs): ctx = super().get_context_data(**kwargs)