cache potential discount information

relevant if shop has multiple categories with cross_selling_condition=discounts
This commit is contained in:
Mira Weller
2024-06-17 14:11:30 +02:00
parent d9f31aae8c
commit 01b535a0af

View File

@@ -164,6 +164,7 @@ class ItemCategory(LoggedModel):
match = set(match.pk for match in self.cross_selling_match_products.only('pk')) # TODO prefetch this 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, {}) return (self.items.all(), {}) if any(pos.item.pk in match for pos in cart) else (None, {})
if self.cross_selling_condition == 'discounts': if self.cross_selling_condition == 'discounts':
if not hasattr(self.event, '_potential_discounts_by_item_for_current_cart'):
potential_discounts_by_cartpos = defaultdict(list) potential_discounts_by_cartpos = defaultdict(list)
from ..services.pricing import apply_discounts from ..services.pricing import apply_discounts
@@ -194,7 +195,7 @@ class ItemCategory(LoggedModel):
next(discount_rule for (item, discount_rule, max_count, i) in infos_for_item) next(discount_rule for (item, discount_rule, max_count, i) in infos_for_item)
) )
grouped_by_item = [ self.event._potential_discounts_by_item_for_current_cart = [
discount_info(item, infos_for_item) for item, infos_for_item in discount_info(item, infos_for_item) for item, infos_for_item in
groupby( groupby(
sorted( sorted(
@@ -211,7 +212,7 @@ class ItemCategory(LoggedModel):
my_item_pks = self.items.values_list('pk', flat=True) my_item_pks = self.items.values_list('pk', flat=True)
potential_discount_items = { potential_discount_items = {
item.pk: (max_count, discount_rule) item.pk: (max_count, discount_rule)
for item, max_count, discount_rule in grouped_by_item for item, max_count, discount_rule in self.event._potential_discounts_by_item_for_current_cart
if max_count > 0 and item.pk in my_item_pks and item.is_available() if max_count > 0 and item.pk in my_item_pks and item.is_available()
} }