In this section you will find practical examples for using vouchers in the checkout.
Voucher entry form with maximumCount check
The entry form is only displayed as long as fewer vouchers have been redeemed than allowed. As soon as the upper limit is reached, the form disappears automatically.
{{ var $cActionVoucherAdd = $wsActions.create("VoucherAdd") }}
{{ include "components/errorAlert.htm" with $cAction = $cActionVoucherAdd, $cViewEachField = true }}
{{ if len($wsVoucher.vouchers) < $wsVoucher.maximumCount }}
<form method="post" action="{{= $wsViews.current.url() }}" data-ws-ajax-form>
<input type="hidden" name="wsReplaceIds" value="wsBasketWrapper,wsBasketEntries,wsBasketOffcanvasContent">
<input type="hidden" name="wsact" value="{{= $cActionVoucherAdd.id }}">
<input type="hidden" name="wscsrf" value="{{= $cActionVoucherAdd.csrf }}>
<input type="hidden" name="wstarget" value="{{= $wsViews.current.url() }}">
<input type="text" name="id" value="" placeholder="Enter voucher code">
<button type="submit">Redeem</button>
</form>
{{ /if }}
Error messages (e.g. “Minimum order value not reached”) are defined and output via components/errorAlert.htm.
Display the list of redeemed vouchers
A separate small form with a unique ID is output for each voucher. Valid vouchers appear green, invalid ones red.
{{ if $wsVoucher.vouchers }}
<p>Redeemed vouchers</p>
{{ foreach $cVoucher in $wsVoucher.vouchers }}
{{ var $cVoucherIsValid = $cVoucher.valid | ifNull(true) }}
{{ var $cActionVoucherDelete = $wsActions.create("VoucherDelete") }}
<form method="post"
action="{{= $wsViews.viewUrl('basket.htm') }}"
data-ws-ajax-form>
<input type="hidden" name="wsReplaceIds" value="wsBasketWrapper,wsBasketEntries,wsBasketOffcanvasContent">
<input type="hidden" name="id" value="{{= $cVoucher.id }}">
<input type="hidden" name="wsact" value="{{= $cActionVoucherDelete.id }}">
<input type="hidden" name="wscsrf" value="{{= $cActionVoucherDelete.csrf }}">
<input type="hidden" name="wstarget" value="{{= $wsViews.current.url() }}">
<span>{{= $cVoucher.id }}</span>
<button type="submit">Remove</button>
</form>
{{ /foreach }}
{{ /if }}
Display all redeemed vouchers in the basket
In this example, all redeemed vouchers are displayed in the basket. This way the customer can transparently see which codes are in the system and which one is currently being applied. Ineffective vouchers are not added to the list and are marked via an error message.
{{ var $cVoucherCount = 0 }}
{{ if $wsVoucher.vouchers }}
{{ foreach $cVoucher in $wsVoucher.vouchers }}
{{ $cVoucherCount = $cVoucherCount + 1 }}
<tr>
<td>
<div>{{ if $cVoucherCount == 1 }}Voucher{{ else }}Additional voucher{{ /if }}</div>
<div>{{= $cVoucher.id }}</div>
</td>
<td>
-{{= $cVoucher.value | currency }}
</td>
</tr>
{{ /foreach }}
{{ /if }}