mirror of
https://github.com/pretix/pretix.git
synced 2026-05-03 14:54:04 +00:00
Add clever handling of plus button in cart with voucher (#2893)
This commit is contained in:
@@ -195,7 +195,7 @@ class CartManager:
|
||||
AddOperation = namedtuple('AddOperation', ('count', 'item', 'variation', 'voucher', 'quotas',
|
||||
'addon_to', 'subevent', 'bundled', 'seat', 'listed_price',
|
||||
'price_after_voucher', 'custom_price_input',
|
||||
'custom_price_input_is_net'))
|
||||
'custom_price_input_is_net', 'voucher_ignored'))
|
||||
RemoveOperation = namedtuple('RemoveOperation', ('position',))
|
||||
VoucherOperation = namedtuple('VoucherOperation', ('position', 'voucher', 'price_after_voucher'))
|
||||
ExtendOperation = namedtuple('ExtendOperation', ('position', 'count', 'item', 'variation', 'voucher',
|
||||
@@ -330,12 +330,16 @@ class CartManager:
|
||||
(isinstance(op, self.ExtendOperation) and op.position.is_bundled)
|
||||
):
|
||||
if op.item.require_voucher and op.voucher is None:
|
||||
if getattr(op, 'voucher_ignored', False):
|
||||
raise CartError(error_messages['voucher_redeemed'])
|
||||
raise CartError(error_messages['voucher_required'])
|
||||
|
||||
if (
|
||||
(op.item.hide_without_voucher or (op.variation and op.variation.hide_without_voucher)) and
|
||||
(op.voucher is None or not op.voucher.show_hidden_items)
|
||||
):
|
||||
if getattr(op, 'voucher_ignored', False):
|
||||
raise CartError(error_messages['voucher_redeemed'])
|
||||
raise CartError(error_messages['voucher_required'])
|
||||
|
||||
if not op.item.is_available() or (op.variation and not op.variation.is_available()):
|
||||
@@ -480,7 +484,7 @@ class CartManager:
|
||||
self._check_item_constraints(op)
|
||||
|
||||
if cp.voucher:
|
||||
self._voucher_use_diff[cp.voucher] += 1
|
||||
self._voucher_use_diff[cp.voucher] += 2
|
||||
|
||||
self._operations.append(op)
|
||||
return err
|
||||
@@ -586,6 +590,7 @@ class CartManager:
|
||||
item = self._items_cache[i['item']]
|
||||
variation = self._variations_cache[i['variation']] if i['variation'] is not None else None
|
||||
voucher = None
|
||||
voucher_ignored = False
|
||||
|
||||
if i.get('voucher'):
|
||||
try:
|
||||
@@ -595,6 +600,24 @@ class CartManager:
|
||||
else:
|
||||
voucher_use_diff[voucher] += i['count']
|
||||
|
||||
if i.get('voucher_ignore_if_redeemed', False):
|
||||
# This is a special case handling for when a user clicks "+" on an existing line in their cart
|
||||
# that has a voucher attached. If the voucher still has redemptions left, we'll add another line
|
||||
# with the same voucher, but if it does not we silently continue as if there was no voucher,
|
||||
# leading to either a higher-priced ticket or an error. Still, this leads to less error cases
|
||||
# than either of the possible default assumptions.
|
||||
predicted_redeemed_after = (
|
||||
voucher.redeemed +
|
||||
CartPosition.objects.filter(voucher=voucher, expires__gte=self.now_dt).count() +
|
||||
self._voucher_use_diff[voucher] +
|
||||
voucher_use_diff[voucher]
|
||||
)
|
||||
if predicted_redeemed_after > voucher.max_usages:
|
||||
i.pop('voucher')
|
||||
voucher_ignored = True
|
||||
voucher = None
|
||||
voucher_use_diff[voucher] -= i['count']
|
||||
|
||||
# Fetch all quotas. If there are no quotas, this item is not allowed to be sold.
|
||||
quotas = list(item.quotas.filter(subevent=subevent)
|
||||
if variation is None else variation.quotas.filter(subevent=subevent))
|
||||
@@ -641,6 +664,7 @@ class CartManager:
|
||||
price_after_voucher=bundle.designated_price,
|
||||
custom_price_input=None,
|
||||
custom_price_input_is_net=False,
|
||||
voucher_ignored=False,
|
||||
)
|
||||
self._check_item_constraints(bop, operations)
|
||||
bundled.append(bop)
|
||||
@@ -670,6 +694,7 @@ class CartManager:
|
||||
price_after_voucher=price_after_voucher,
|
||||
custom_price_input=custom_price,
|
||||
custom_price_input_is_net=self.event.settings.display_net_prices,
|
||||
voucher_ignored=voucher_ignored,
|
||||
)
|
||||
self._check_item_constraints(op, operations)
|
||||
operations.append(op)
|
||||
@@ -801,6 +826,7 @@ class CartManager:
|
||||
price_after_voucher=listed_price,
|
||||
custom_price_input=custom_price,
|
||||
custom_price_input_is_net=self.event.settings.display_net_prices,
|
||||
voucher_ignored=False,
|
||||
)
|
||||
self._check_item_constraints(op, operations)
|
||||
operations.append(op)
|
||||
|
||||
@@ -267,6 +267,10 @@
|
||||
data-asynctask-text="{% blocktrans with time=event.settings.reservation_time %}Once the items are in your cart, you will have {{ time }} minutes to complete your purchase.{% endblocktrans %}"
|
||||
method="post" data-asynctask>
|
||||
<input type="hidden" name="subevent" value="{{ line.subevent_id|default_if_none:"" }}" />
|
||||
{% if line.voucher and not line.voucher.seat %}
|
||||
<input type="hidden" name="_voucher_code" value="{{ line.voucher.code }}" />
|
||||
<input type="hidden" name="_voucher_ignore_if_redeemed" value="on" />
|
||||
{% endif %}
|
||||
{% csrf_token %}
|
||||
{% if line.variation %}
|
||||
<input type="hidden" name="variation_{{ line.item.id }}_{{ line.variation.id }}"
|
||||
|
||||
@@ -136,7 +136,7 @@ class CartActionMixin:
|
||||
except InvoiceAddress.DoesNotExist:
|
||||
return InvoiceAddress()
|
||||
|
||||
def _item_from_post_value(self, key, value, voucher=None):
|
||||
def _item_from_post_value(self, key, value, voucher=None, voucher_ignore_if_redeemed=False):
|
||||
if value.strip() == '' or '_' not in key:
|
||||
return
|
||||
|
||||
@@ -161,6 +161,7 @@ class CartActionMixin:
|
||||
'seat': value,
|
||||
'price': price,
|
||||
'voucher': voucher,
|
||||
'voucher_ignore_if_redeemed': voucher_ignore_if_redeemed,
|
||||
'subevent': subevent
|
||||
}
|
||||
except ValueError:
|
||||
@@ -183,6 +184,7 @@ class CartActionMixin:
|
||||
'count': amount,
|
||||
'price': price,
|
||||
'voucher': voucher,
|
||||
'voucher_ignore_if_redeemed': voucher_ignore_if_redeemed,
|
||||
'subevent': subevent
|
||||
}
|
||||
except ValueError:
|
||||
@@ -195,6 +197,7 @@ class CartActionMixin:
|
||||
'count': amount,
|
||||
'price': price,
|
||||
'voucher': voucher,
|
||||
'voucher_ignore_if_redeemed': voucher_ignore_if_redeemed,
|
||||
'subevent': subevent
|
||||
}
|
||||
except ValueError:
|
||||
@@ -219,7 +222,8 @@ class CartActionMixin:
|
||||
for key, values in req_items:
|
||||
for value in values:
|
||||
try:
|
||||
item = self._item_from_post_value(key, value, self.request.POST.get('_voucher_code'))
|
||||
item = self._item_from_post_value(key, value, self.request.POST.get('_voucher_code'),
|
||||
voucher_ignore_if_redeemed=self.request.POST.get('_voucher_ignore_if_redeemed') == 'on')
|
||||
except CartError as e:
|
||||
messages.error(self.request, str(e))
|
||||
return
|
||||
|
||||
@@ -327,6 +327,31 @@ class CartTest(CartTestMixin, TestCase):
|
||||
objs = list(CartPosition.objects.filter(cart_id=self.session_key, event=self.event))
|
||||
self.assertEqual(len(objs), 0)
|
||||
|
||||
def test_voucher_ignore_if_Redeemed(self):
|
||||
with scopes_disabled():
|
||||
v = Voucher.objects.create(item=self.ticket, event=self.event, max_usages=2)
|
||||
self.client.post('/%s/%s/cart/add' % (self.orga.slug, self.event.slug), {
|
||||
'item_%d' % self.ticket.id: '1',
|
||||
'_voucher_code': v.code,
|
||||
'_voucher_ignore_if_redeemed': 'on',
|
||||
}, follow=True)
|
||||
self.client.post('/%s/%s/cart/add' % (self.orga.slug, self.event.slug), {
|
||||
'item_%d' % self.ticket.id: '1',
|
||||
'_voucher_code': v.code,
|
||||
'_voucher_ignore_if_redeemed': 'on',
|
||||
}, follow=True)
|
||||
self.client.post('/%s/%s/cart/add' % (self.orga.slug, self.event.slug), {
|
||||
'item_%d' % self.ticket.id: '1',
|
||||
'_voucher_code': v.code,
|
||||
'_voucher_ignore_if_redeemed': 'on',
|
||||
}, follow=True)
|
||||
with scopes_disabled():
|
||||
objs = list(CartPosition.objects.filter(cart_id=self.session_key, event=self.event).order_by('id'))
|
||||
self.assertEqual(len(objs), 3)
|
||||
self.assertEqual(objs[0].voucher, v)
|
||||
self.assertEqual(objs[1].voucher, v)
|
||||
self.assertIsNone(objs[2].voucher)
|
||||
|
||||
def test_voucher_subevent(self):
|
||||
self.event.has_subevents = True
|
||||
self.event.save()
|
||||
|
||||
Reference in New Issue
Block a user