1

I am trying to display the Discount Value (%) of Products with the onSale Tag. What works for now is to display only the Products which are onSale. But I can't figure out which value stores the Discount Value.

{% set products = craft.commerce.products.find() %} {% for product in products %}
<li class="list-item">
    {% if product.defaultVariant.onSale == true %}
    <div class="pin circle">
        <h6>Sale!</h6>
        <h6 class="percent">{{ amount.saleAmount }}/h6>
        <h6>off</h6>
    </div>
    {% endif %}

2

I haven't done this myself, but I see two ways to get the percentage:

Take the product.defaultVariant.saleAmount and use maths in twig to work out the percentage - that should definitely work. Twig math is a good general solution if you can't get the numbers you need directly from Commerce. See:

http://twig.sensiolabs.org/doc/templates.html#math

However, I think you can use

 {% set sales = craft.commerce.getSales() %}

...to get all your SalesModels.

(see /craft/plugins/commerce/models/Commerce_SaleModel.php) as this model is not really documented yet it seems.

You'd loop through those and get the particular sale you're after (e.g. by id). Then, it appears there is code in there to getDiscountAmountAsPercent to get what you're after....however while DiscountModel has an attribute of percentDiscount on it, SaleModel does not appear to so I don't think you can get at it directly in your templates currently.

Unfortunately I am a bit pushed for time in terms of actually trying it to see. But Twig maths will do it for sure if you get stuck with a more direct approach!

| improve this answer | |
  • thanks a lot, i will try to figure it out tomorrow, i actually just need the value that the costumer enters to display something like a sticker on the product image to say how much % is off ..maybe there is a workaround if the costumer just names the deal 20 for 20% and i can read out the deal name i.e. – HendrikEng Oct 28 '16 at 0:40
  • will try to work it out and report here, thanks a lot :-) – HendrikEng Oct 28 '16 at 0:41
2

For some reason the above answers aren't working for me, so I've done it the mathematical way...

Formula: ((newprice-oldprice)/oldprice)*100

In craft/twig code:

{% if product.defaultVariant.onSale %}
  <span class="badge">
    {% set procent = ((product.defaultVariant.salePrice-product.defaultPrice)/product.defaultPrice)*100 %}
    {{procent}}%
  </span>
{% endif %}

Hope it helps for those who where struggling also... :)

| improve this answer | |
1

I believe saleAmount is what you're looking for.

{% set amount = product.defaultVariant.saleAmount %}
| improve this answer | |
  • i checked the docs again, i think you are right, i figured its saleAmount or discount but i cant cant it working, i want do output the value i entered in the dashboard (i.e. 20%) i updated the code above. – HendrikEng Oct 28 '16 at 0:36

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.