1

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?

2

Following updated code sort the problem mention above. I hope this will help someone. (If you have modification or error please comment)

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);
                $stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($item->getProductId());

                    if (((int)$stockItem->getQty() - (int)$canceled) <= (int)$stockItem->getMinQty()) {
                        $stockItem->setIsInStock(false)
                            ->setStockStatusChangedAutomaticallyFlag(true);
                    }
                    $stockItem->save();
            }
            $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 = 'processing';
        $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()));
}

}

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.