Allow to redeem a voucher for an existing cart (#1517)

* Allow to redeem a voucher for an existing cart

* Bundle behaviour
This commit is contained in:
Raphael Michel
2019-12-11 15:58:22 +01:00
committed by GitHub
parent 352942b7d6
commit 99f3db04a9
8 changed files with 365 additions and 3 deletions

View File

@@ -246,11 +246,31 @@
<div class="product">
<strong>{% trans "Total" %}</strong>
</div>
<div class="count hidden-xs">
<div class="count hidden-xs hidden-sm">
<strong>{{ cart.itemcount }}</strong>
</div>
<div class="col-md-3 col-xs-6 col-md-offset-3 price">
<strong>{{ cart.total|money:event.currency }}</strong>
{% if editable and show_vouchers and not cart.all_with_voucher %}
<br>
<a class="js-only apply-voucher-toggle" href="#">
<span class="fa fa-tag"></span> {% trans "Redeem a voucher" %}
</a>
<form action="{% eventurl event "presale:event.cart.voucher" cart_namespace=cart_namespace|default_if_none:"" %}"
data-asynctask-headline="{% trans "We're applying this voucher to your cart..." %}"
method="post" data-asynctask class="apply-voucher">
{% csrf_token %}
<div class="input-group">
<input type="text" class="form-control" name="voucher" placeholder="{% trans "Voucher code" %}">
<span class="input-group-btn">
<button class="btn btn-primary" type="submit">
<span class="fa fa-check"></span>
</button>
</span>
</div>
</form>
{% endif %}
</div>
<div class="clearfix"></div>
</div>

View File

@@ -18,6 +18,7 @@ import pretix.presale.views.widget
frame_wrapped_urls = [
url(r'^cart/remove$', pretix.presale.views.cart.CartRemove.as_view(), name='event.cart.remove'),
url(r'^cart/voucher$', pretix.presale.views.cart.CartApplyVoucher.as_view(), name='event.cart.voucher'),
url(r'^cart/clear$', pretix.presale.views.cart.CartClear.as_view(), name='event.cart.clear'),
url(r'^cart/answer/(?P<answer>[^/]+)/$',
pretix.presale.views.cart.AnswerDownload.as_view(),

View File

@@ -164,6 +164,7 @@ class CartMixin:
return {
'positions': positions,
'all_with_voucher': all(p.voucher_id for p in positions),
'raw': cartpos,
'total': total,
'net_total': net_total,

View File

@@ -23,7 +23,8 @@ from pretix.base.models import (
CartPosition, InvoiceAddress, QuestionAnswer, SubEvent, Voucher,
)
from pretix.base.services.cart import (
CartError, add_items_to_cart, clear_cart, remove_cart_position,
CartError, add_items_to_cart, apply_voucher, clear_cart,
remove_cart_position,
)
from pretix.base.views.tasks import AsyncAction
from pretix.multidomain.urlreverse import eventreverse
@@ -327,6 +328,26 @@ def cart_session(request):
return request.session['carts'][cart_id]
@method_decorator(allow_frame_if_namespaced, 'dispatch')
class CartApplyVoucher(EventViewMixin, CartActionMixin, AsyncAction, View):
task = apply_voucher
known_errortypes = ['CartError']
def get_success_message(self, value):
return _('We applied the voucher to as many products in your cart as we could.')
def post(self, request, *args, **kwargs):
if 'voucher' in request.POST:
return self.do(self.request.event.id, request.POST.get('voucher'), get_or_create_cart_id(self.request), translation.get_language())
else:
if 'ajax' in self.request.GET or 'ajax' in self.request.POST:
return JsonResponse({
'redirect': self.get_error_url()
})
else:
return redirect(self.get_error_url())
@method_decorator(allow_frame_if_namespaced, 'dispatch')
class CartRemove(EventViewMixin, CartActionMixin, AsyncAction, View):
task = remove_cart_position