2

starting point

I have the following offer with quantity discounts for a series of my products:

  • Buy 1 item and get $5 off per item
  • Buy 2 items and get $10 off per item
  • Buy 3 items and get $15 off per item

I set these discount rules in my control panel under "commerce" > "promotions".


product template

Now in my product template, I would like to be able to output (for each quantity level):

  1. the normal (non-discounted) price
  2. the discounted price
  3. savings in terms of $
  4. savings in terms of %

example

For example it would then say on the product page (output):

"If you buy 2 items, you only pay $[discount price for 2 items] instead of $[normal price] per item. So your total today is only $[2 x discount price for 2 items] insted of $[2 x normal price]. You therefore save $[(2 x normal price)-(2 x discount price for 2 items)]. That's a discount of […]%."

The same goes for the discount offer of ordering 1 item or 3 items.


How can I accomplish this with all the discounts and math operations involved?

1 Answer 1

3

What have you tried so far?

The documentation seems to be missing, but looking at /craft/plugins/commerce/models/Commerce_DiscountModel.php you can find the properties purchaseQty and perItemDiscount that you need for the calculations.

{% for prod in product.variants %}
   {% for discount in craft.commerce.discounts %}
      {% set totalPrice = (discount.purchaseQty * prod.price) %}
      {% set totalDiscountPrice = (discount.purchaseQty * (prod.price + discount.perItemDiscount)) %}
      {% set totalDiscount = (totalPrice-totalDiscountPrice) %}
      {% set totalDiscountPercent = (totalDiscount/totalPrice)*100 %}

      <p>If you buy {{ discount.purchaseQty }} items, you only pay {{ totalDiscountPrice|currency(cart.currency) }} instead of {{ prod.price|currency(cart.currency) }} per item. So your total today is only {{ totalDiscountPrice|currency(cart.currency) }} insted of {{ totalPrice|currency(cart.currency) }}. You therefore save {{ totalDiscount|currency(cart.currency) }}. That's a discount of {{ totalDiscountPercent|round }}%.</p>
   {% endfor %}
{% endfor %}

This will show all of the discounts for all of the products. You will need to modify it slightly if you have specified the discount for a specific product. You also need to check if the discount is enabled or not using discount.enabled

5
  • Hi Alex, it works great! I have added this: {% set discountPricePerItem = (prod.price + discount.perItemDiscount) %} to be able to display the "per item"-price on each discount level. Many thanks.
    – Patrick
    May 31, 2016 at 13:13
  • Hi Alex, I have specific discount rules for each product separately. How can I modify this code so it would show ONLY the product specific discounts and NOT all the discounts?
    – Patrick
    Mar 30, 2017 at 10:54
  • Also, {% if discount.enabled == true %} doesn't work. Do I need to write this differently?
    – Patrick
    Mar 30, 2017 at 10:56
  • Can you please open it as a new question so that I can give you a proper answer rather than just in a comment? Mar 30, 2017 at 10:56
  • I used this to filter product specific discounts: {% if product.id in discount.productIds or product.typeId in discount.productTypeIds %} – and it works
    – Patrick
    Mar 30, 2017 at 11:11

Your Answer

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

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