0
1.{% set totalDeliveryTime = 0 %}
2.{% set countDeliveries = 0 %}
3.{% set averageDeliveryTime = 0 %}    
4.{% for order in pastOrders %}
5. to--{{order.orderDeliveryTime}}--
6.{% set totalDeliveryTime = totalDeliveryTime + order.orderDeliveryTime %}
7.  td--{{totalDeliveryTime}}
8.{% set countDeliveries = countDeliveries + 1 %}
9.  tc--{{countDeliveries}}
10.{% endfor %}
11.  result--{{ totalDeliveryTime / countDeliveries }}

Result: to--15-- td--1 tc--1

to--30-- td--2 tc--2

to--60-- td--3 tc--3

to--60-- td--4 tc--4

result--1

Its a simple average calculating formula. I want to add my all 'orderDeliveryTime' in 'totalDeliveryTime' through line 6. As in line 5, it returns correct values of orderDeliveryTime which is in 'to' you can see in Result but when we add in totalDeliveryTime through line 6 it return 'td' values (please look 'td' in Result). I think it should 15+30+60+60 (addition of all orderDeliveryTime).

Beside this I also used

{% set ratingsSum = 0 %}
{% if pastOrders | length %}
{% for order in pastOrders %}
{% set ratingsSum = ratingsSum + order.orderDeliveryTime %}
{% endfor %}
{% set ratingAverage = ratingsSum / pastOrders | length %}
AverageTime:{{ratingAverage}}
{% endif %}

https://cliveportman.co.uk/code/calculate-average-of-numbers-in-craft-cms-using-twig But it also returns the same result which is 1.

  • What is the actual question? I just see a bunch of code with virtually no explanation. – Lindsey D Mar 22 '17 at 19:38
  • 1
    What type of variable is order.orderDeliveryTime? Somehow order.orderDeliveryTime is returning 1 instead of the actual number when Twig is doing addition. If you dump(order.orderDeliveryTime) what do you see? Not sure if casting it would work? eg totalDeliveryTime + order.orderDeliveryTime|number_format – RitterKnight Mar 23 '17 at 20:48
  • Actually orderDeliveryTime Getting the values from dropdown. – Mohit Prajapati Mar 24 '17 at 7:11
1

You can try the following code

{% set ratingsSum = 0 %}
{% for order in pastOrders %}
    {% set ratingsSum = ratingsSum + order.orderDeliveryTime.value %}
{% endfor %}

{% set ratingAverage = ratingsSum / pastOrders | length %}
AverageTime:{{ratingAverage}}

Difference between yours and my code is i have added .value with order.orderDeliveryTime

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.