1

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?

1 Answer 1

2

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....)

1
  • Thanks for sharing your method! Commented Nov 15, 2020 at 22:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.