Skip to content

Commit

Permalink
feat: final integration gift card vouchers
Browse files Browse the repository at this point in the history
  • Loading branch information
nedu64 committed Nov 11, 2024
1 parent 0c4aae3 commit a45f71f
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
15 changes: 13 additions & 2 deletions src/templates/_components/app/summary-cart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,12 @@
{% if voucherCodes|length >= 1 %}
<div class="border-gray-400 border-b flex flex-col space-y-2 pb-4">
<h3 class="font-semibold text-gray-800">{{ 'Gift cards applied'|t('foster-checkout') }}</h3>

{% set voucherAmountApplied = 0 %}
{% set totalVoucherAmountApplied = 0 %}
{% set appliedAmount = 0 %}
{% for code in voucherCodes %}
{# #}
{% set voucherCurrentAmount = craft.giftVoucher.codes().codeKey(code).one().currentAmount %}

<div class="flex justify-between items-center">
<dt class="text-gray-500">{{ code }}</dt>
Expand All @@ -77,7 +81,14 @@
</button>
</form>
</dd>
<dd class="text-right text-red-500">- {{ craft.giftVoucher.codes().codeKey(code).one().currentAmount | currency }}</dd>
{% if cart.storedItemTotal - voucherCurrentAmount > 0 %}
{% set voucherAmountApplied = voucherAmountApplied + voucherCurrentAmount %}
{% set appliedAmount = (cart.storedItemTotal - voucherAmountApplied) <= 0 ? voucherAmountApplied - cart.storedItemTotal : voucherCurrentAmount %}
{% set totalVoucherAmountApplied = voucherAmountApplied %}
{% else %}
{% set appliedAmount = (totalVoucherAmountApplied - cart.storedItemTotal)|abs %}
{% endif %}
<dd class="text-right text-red-500">- {{ appliedAmount | currency }}</dd>
</div>

{% endfor %}
Expand Down
31 changes: 24 additions & 7 deletions src/templates/checkout/order.twig
Original file line number Diff line number Diff line change
Expand Up @@ -68,20 +68,37 @@
</div>
{% endif %}

<div class="sm:grid sm:grid-cols-[90px_1fr] sm:gap-6">
<dt class="font-semibold">{{ 'Payment'|t('foster-checkout') }}</dt>
<dd>{{ order.gateway.name }}</dd>
</div>

{% if craft.giftVoucher is defined %}
{# Check for applied / redeemed gift card vouchers in orders #}

{% if order.orderAdjustments|length >= 1 %}
<div style="width: fit-content" class="p-5 border text-green-900 bg-green-100 border-green-100 inline-block">
<span>Gift Voucher Applied</span>
{#
We currently only have access to the redeemed gift card vouchers code using order.orderAdjustments[0].description
which appears in the form "Gift Voucher discount using code MTLYZKVDAQ"
In order to get the code, we'll have to split the description string using 'code ' as its delimeter
#}

<div class="sm:grid sm:grid-cols-[90px_1fr] sm:gap-6">
<dt class="font-semibold">{{ 'Gift card applied'|t('foster-checkout') }}</dt>
<dd>
{% for voucher in order.orderAdjustments %}
{# Get redeemed voucher code from description #}
{% set voucherCode = voucher.description|split('code ')[1] %}
{# Get voucher remaining amount #}
{% set amountRemaining = craft.giftVoucher.codes().codeKey(voucherCode).one().currentAmount %}
{% set appliedVoucherAmount = (order.storedItemTotal - (voucher.amount|abs)) < 0 ? order.storedItemTotal : voucher.amount|abs %}
{% set appliedVoucherDescription = voucher.description|split('code ')[1] ~ ' (' ~ appliedVoucherAmount|currency ~ ' applied, ' ~ amountRemaining|currency ~ ' remaining)' %}
<p>{{ appliedVoucherDescription }}</p>
{% endfor %}
</dd>
</div>
{% endif %}
{% endif %}

<div class="sm:grid sm:grid-cols-[90px_1fr] sm:gap-6">
<dt class="font-semibold">{{ 'Payment'|t('foster-checkout') }}</dt>
<dd>{{ order.gateway.name }}</dd>
</div>
</dl>

<div class="flex justify-end items-center gap-6 mt-9 lg:mt-12">
Expand Down
36 changes: 27 additions & 9 deletions src/templates/checkout/payment.twig
Original file line number Diff line number Diff line change
Expand Up @@ -251,16 +251,34 @@

{% set voucherCodes = craft.giftVoucher.getVoucherCodes() %}

{% if voucherCodes|length >= 1 %}
<div class="border-gray-400 border-b flex flex-col space-y-2 py-4">

{% for code in voucherCodes %}

<div class="flex justify-between items-center">
<p class="text-gray-500">{{ 'Gift cards currently applied to your order'|t('foster-checkout') }}: {{ code }} ({{ craft.giftVoucher.codes().codeKey(code).one().currentAmount | currency }})</p>
</div>
<div class="border-gray-400 border-b flex flex-col space-y-2 py-4 px-4 xl:px-0">
{% if voucherCodes|length == 1 %}
{% set currentAmount = craft.giftVoucher.codes().codeKey(voucherCodes[0]).one().currentAmount %}
{% set appliedAmount = (cart.storedItemTotal - currentAmount) <= 0 ? currentAmount - cart.storedItemTotal : currentAmount %}
<div class="flex justify-between items-center">
<p class="text-gray-500">{{ 'Gift cards currently applied to your order'|t('foster-checkout') }}: {{ voucherCodes[0] }} ({{ appliedAmount | currency }})</p>
</div>
{% endif %}

{% endfor %}
{# Applied gift card greater than 1 #}
{% if voucherCodes|length > 1 %}
{% set voucherAmountApplied = 0 %}
{% set totalVoucherAmountApplied = 0 %}
{% set appliedAmount = 0 %}
<p class="text-gray-500 m-0">{{ 'Gift cards currently applied to your order'|t('foster-checkout') }}:</p>
<ul class="text-gray-500 m-0 list-disc">
{% for code in voucherCodes %}
{% set voucherCurrentAmount = craft.giftVoucher.codes().codeKey(code).one().currentAmount %}
{% if cart.storedItemTotal - voucherCurrentAmount > 0 %}
{% set voucherAmountApplied = voucherAmountApplied + voucherCurrentAmount %}
{% set appliedAmount = (cart.storedItemTotal - voucherAmountApplied) <= 0 ? voucherAmountApplied - cart.storedItemTotal : voucherCurrentAmount %}
{% set totalVoucherAmountApplied = voucherAmountApplied %}
{% else %}
{% set appliedAmount = (totalVoucherAmountApplied - cart.storedItemTotal)|abs %}
{% endif %}
<li class="ml-5">{{ code }} ({{ appliedAmount | currency }})</li>
{% endfor %}
</ul>
</div>
{% endif %}
</div>
Expand Down

0 comments on commit a45f71f

Please sign in to comment.