So on a cart page, I need to list out the discounts that have been applied to a cart. I need to list them one by one, but only discounts that are applied with a code. I'm having trouble figuring out how to get the discount code, and I need to output the code. Something like this:
Item Total: $200.00
Promotional (25OFF): -$25.00
Promotional (FATHERSDAY): -$10.00
...
I'm looking over the discount info like this:
{% for discount in cart.adjustments %}
{{ dump(discount) }}
<br>
{% endfor %}
I don't see an accessible code variable jumping out at me. The only places I can see pulling it is from the optionsJson
string, but that is not practical as Twig doesn't have a default JSON filter to decode.
So I can get the discount ID easily enough from that loop; but I'm not sure how to pull a Discount model based on ID. I'm not super versed with Craft and Twig, so I'm hoping there is a simple solution to look up Discounts based on discount ID.
So like this:
{% for discounts in cart.adjustments %}
{# look up discount based on discount.id #}
{% if discount.code | length %}
Promotional ({{ discount.code }}): {# discount amount, probably have to do math here if it is a percentage #}
{% endif %}
{% endfor %}
{% endfor %}
I imagine I'm going about this wrong.