1

It's easy to know if a product has a valid Sale applied to it with product.defaultVariant.onSale - but how do you know which Sales are being applied to the product? How do you output that the product is currently part of a specific named sale, and any associated content you might want to show?

3

The real answer is:

{% for sale in variant.sales %}

    {% sale.description %}

{% endfor %}
4
  • Thanks Luke; that's not something I could find in the docs for either the product or sale documentation pages. I tried to check again just but the Commerce site has been offline all day. Feb 23 '18 at 14:44
  • @MattWilcox I have updated the docs. Thanks. Feb 26 '18 at 4:17
  • @LukeHolder how does this work in Commerce 3, I get: Calling unknown method: craft\commerce\elements\Variant::salesApplied()
    – Green17
    May 20 at 12:46
  • 1
    @Green17 It would be for sale in variant.sales May 27 at 8:00
1

Thanks to Mike Pepper on the Slack chat, this is the answer. A bit disappointing that we need to go through hoops like this, but it works.

{% set productSales = [] %}
{% set allSales     = craft.commerce.getSales() %}

{% for sale in allSales if product.id in sale.productIds or product.typeId in sale.productTypeIds %}
    {% set productSales = productSales|merge([sale]) %}
{% endfor %}

{% for sale in productSales if product.defaultVariant.onSale %}
    <div class="sale">
        <p><b>{{ sale.name }}</b> - {{ sale.description }}</p>
    </div>
{% endfor %}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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