TransWikia.com

Commerce - show description of discount without coupon applied to cart

Craft CMS Asked on December 1, 2020

We can retrieve description of discount applied to cart that has its own coupon like this:

craft.commerce.discounts.getDiscountByCode(cart.couponCode).description

Is it possible for discount that do not have coupons and are applied automatically?

One Answer

I suspect there is a much better way, but I use something like this, looping over all cart adjustments, checking the type of adjustment.

We use the adjustment.sourceSnapshot for the coupon code, otherwise access the attributes on the adjustment directly, which is a Discount model.

        {% for adjustment in cart.adjustments if adjustment.type == "discount" %}
            {% set isCoupon = adjustment.sourceSnapshot.code ??? false %}              
            {% if isCoupon %}
                Coupon: {{ adjustment.sourceSnapshot.code }}
            {% else %}
                {{ adjustment.name }}<br />
                {{ adjustment.description }}
            {% endif %}

         {% endfor %}

...that works.

Documentation, such as it is, is here: https://craftcms.com/docs/commerce/3.x/adjusters.html#adjusting

And the available attributes for a discount model: https://docs.craftcms.com/commerce/api/v3/craft-commerce-models-discount.html#app

(or src: https://github.com/craftcms/commerce/blob/master/src/models/Discount.php)

(Honestly, though, even looking at the documentation now, it's really not obvious how to come to this code....)

Correct answer by Jeremy Daalder on December 1, 2020

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP