Apparently I've found a bug in Magento, and

I'm creating an order on frontend with 2 different products. Qty ordered is greater than 1 for both products. Last test on was with 2 simple products. I invoice in admin only 1 product. Than I click Credit Memo button and I get this:

The warning is from

Warning: Division by zero  in app/code/core/Mage/Sales/Model/Order/Creditmemo/Item.php on line 266

#0 app/code/core/Mage/Sales/Model/Order/Creditmemo/Item.php(266): mageCoreErrorHandler(2, 'Division by zer...', '/nethosting/mag...', 266, Array)
#1 app/code/core/Mage/Sales/Model/Order/Creditmemo/Total/Subtotal.php(48): Mage_Sales_Model_Order_Creditmemo_Item->calcRowTotal()
#2 app/code/core/Mage/Sales/Model/Order/Creditmemo.php(330): Mage_Sales_Model_Order_Creditmemo_Total_Subtotal->collect(Object(Mage_Sales_Model_Order_Creditmemo))
#3 app/code/core/Mage/Sales/Model/Service/Order.php(240): Mage_Sales_Model_Order_Creditmemo->collectTotals()
#4 app/code/core/Mage/Adminhtml/controllers/Sales/Order/CreditmemoController.php(138): Mage_Sales_Model_Service_Order->prepareCreditmemo(Array)
#5 app/code/core/Mage/Adminhtml/controllers/Sales/Order/CreditmemoController.php(222): Mage_Adminhtml_Sales_Order_CreditmemoController->_initCreditmemo()
#6 app/code/core/Mage/Core/Controller/Varien/Action.php(418): Mage_Adminhtml_Sales_Order_CreditmemoController->newAction()
#7 app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('new')
#8 app/code/core/Mage/Core/Controller/Varien/Front.php(172): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http))
#9 app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch()
#10 app/Mage.php(684): Mage_Core_Model_App->run(Array)
#11 index.php(87): Mage::run('', 'store')
#12 {main}

In Mage_Sales_Model_Order_Creditmemo_Item::calcRowTotal on line 265-266:

$availableQty = $orderItemQtyInvoiced - $orderItem->getQtyRefunded();
$rowTotal     = $creditmemo->roundPrice($rowTotal / $availableQty * $this->getQty());

Ok, the second product/item has invoiced qty 0, such so available qty becomes 0. A divide by 0 raises the warning above. If I try to fix these lines locally than it will trigger another warning (devide by zero) in other part of the code (Tax.php). I could try to 'fix' these in core files but I'm still not convinced it will work eventually.

Why all order items are considered in credit memo even if some of the items aren't invoiced ? Anyone knows a good approach for a fix of this issue ?

I guess overall it's not a big deal since this is a warning and on production servers warnings are logged in var/log at most.

Thank you

1 Answer 1


I don't believe this is a common problem to Magento order processing, it may be specific to an order. Try some test orders on your dev box and see if you can refund them.

If you are really stuck and cannot find a solution, here is a comical way of doing it, the numbers should add up fine:

$rowTotal     = $creditmemo->roundPrice($rowTotal / ($availableQty * $this->getQty() + 0.00000000001));

For such a lousy solution you probably wouldn't want to be making a module, just copy the app/code/core/Mage/Sales/Model/Order/Creditmemo/Item.php to app/code/local/Mage/Sales/Model/Order/Creditmemo/Item.php and rely on Magento's factory methods to pick up your amended file.

You can do this for tax.php etc, then, if you are not using 'app/code/local/Mage' for anything else, remove those files once a proper solution is found (or) the problem orders are out the way.

  • Thank you Woolfie. I'll try to apply a fix in both classes. I'll post my results. Your code looks cool but I think we can do this by comparing available qty with 0 too. Like set row total to 0 or null if available qty is <= 0. Sep 18, 2014 at 11:50
  • My answer was inspired by an answer in this epic question: stackoverflow.com/questions/184618/… Sep 18, 2014 at 12:09
  • What was the answer in the link above ? Sep 18, 2014 at 12:50
  • It is in one of the 518 answers there, probably a few pages in but not towards the end because the comments at the end are lame. Sep 18, 2014 at 13:58
  • If you can't tell which is the exact reference than why tell about it ? Sep 18, 2014 at 16:26

Your Answer

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

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