I am trying to develop undo cancelled order extension. For that I am extending OrderController.php.
class Extension_Undocancel_Adminhtml_Sales_OrderController extends Mage_Adminhtml_Controller_Action
{
public function undocancelAction()
{
$id = $this->getRequest()->getParam('order_id');
$order = Mage::getModel('sales/order')->load($id);
$items = $order->getItemsCollection();
try {
foreach($items as $item) {
$canceled = $item->getQtyCanceled();
if($canceled>0) {
$productUpdates[$item->getProductId()] = array('qty' => $canceled);
}
$item->setData('qty_canceled',0);
}
Mage::getSingleton('cataloginventory/stock')->registerProductsSale($productUpdates);
$items->save();
$order->setBaseDiscountCanceled(0)
->setBaseShippingCanceled(0)
->setBaseSubtotalCanceled(0)
->setBaseTaxCanceled(0)
->setBaseTotalCanceled(0)
->setDiscountCanceled(0)
->setShippingCanceled(0)
->setSubtotalCanceled(0)
->setTaxCanceled(0)
->setTotalCanceled(0);
$state = 'new';
$status = 'processing';
$order->setStatus($status)
->setState($state)
->save();
$this->_getSession()->addSuccess($this->__('Order was successfully uncancelled.'));
}
catch (Mage_Core_Exception $e) {
$this->_getSession()->addError($e->getMessage());
}
catch (Exception $e) {
$this->_getSession()->addError($this->__('Order was not uncancelled.'));
}
$this->_redirect('*/sales_order/view', array('order_id' => $order->getId()));
}
}
It works fine even update product stock as per canceled quantity but If product quantity become zero it still show in stock.
How I can update stock availability if qty becomes zero?