@@ -157,20 +157,22 @@ public function changeIdentity(Varien_Event_Observer $event)
157157
158158 if (is_object ($ order_totals )) {
159159 $ order_totals
160- ->addFieldToSelect ('* ' )
161- ->addFieldToFilter ('customer_id ' , $ customer ->getId ())
162- ->addFieldToFilter ('status ' , Mage_Sales_Model_Order::STATE_COMPLETE )
163- ->addAttributeToSelect ('grand_total ' )
164- ->getColumnValues ('grand_total ' );
160+ ->addFieldToFilter ('customer_id ' , $ customer ->getId ())
161+ ->addFieldToFilter ('status ' , Mage_Sales_Model_Order::STATE_COMPLETE );
165162
166- $ sum = 0 ;
167- foreach ($ order_totals as $ total ) {
168- if (isset ($ total ['grand_total ' ]))
169- $ sum += (float )$ total ['grand_total ' ];
170- }
163+ $ order_totals ->getSelect ()
164+ ->reset (Zend_Db_Select::COLUMNS )
165+ ->columns (new Zend_Db_Expr ("SUM(grand_total) as total " ))
166+ ->columns (new Zend_Db_Expr ("AVG(grand_total) as avg_total " ))
167+ ->group ('customer_id ' );
168+
169+ if (count ($ order_totals ) > 0 ) {
170+ $ sum = (float ) $ order_totals ->getFirstItem ()->getTotal ();
171+ $ avg = (float ) $ order_totals ->getFirstItem ()->getAvgTotal ();
171172
172- $ lifetime_sale = Mage::helper ('core ' )->currency ($ sum , true , false );
173- $ average_sale = Mage::helper ('core ' )->currency ($ sum / (count ($ order_totals ) ?: 1 ), true , false );
173+ $ lifetime_sale = Mage::helper ('core ' )->currency ($ sum , true , false );
174+ $ average_sale = Mage::helper ('core ' )->currency ($ avg , true , false );
175+ }
174176 }
175177
176178 $ info ['user ' ] = array (
0 commit comments