1

I have installed Magento 1.8. When a user orders a product, the quantity is decreased by one, but if he cancels that order (before invoicing), the stock quantity does not gets increased.

Admin > Catalog > Inventory > Stock Options > Set Items Status to be In Stock When Order is Cancelled is set to Yes

The code I have been using to cancel order is as follows:

class Kaushikamdotcom_Pay_OrderController extends Mage_Core_Controller_Front_Action {
    public function cancelAction() {
        if (($order_id = $this->getRequest()->getParam('order_id'))) {
            $order = Mage::getModel('sales/order')->load($order_id);

            $order->setState(Mage_Sales_Model_Order::STATE_CANCELED, true);
            $order->save();
        }

        $this->_redirect('sales/order/history');
    }
}

The code I have been using to know the current stock is as follows (The same can be also seen in the admin side):

Mage::getModel('cataloginventory/stock_item')->loadByProduct($_product)->getQty();

Why is the quantity not get increased ? Is it the intended behavior ? I searched in google, on those links I have been instructed to do it programmatically.

  • Please check Backorders is enabled in product -inventory tab.... – Amit Bera Mar 15 '14 at 12:39
1

You can try below code for cancelling an order.

$orderModel = Mage::getModel('sales/order');
$orderModel->load($orderId);
if(!$orderModel->canCancel()) {
$orderModel->cancel();
$orderModel->setState(Mage_Sales_Model_Order::STATE_CANCELED, true)->save();
$orderModel->save();
}

In your code, actual code is missing for cancel the order, you had never called cancel() function of class Mage_Sales_Model_Order

1

I was only changing the state of the order. That was not the correct way to do cancellation. I have found out an elegant way to do that here.

The relevant code from this tutorial is:

if(!$order->canCancel()) {
    throw new Exception('Order cannot be canceled anymore.');
}
$order->cancel();
$order->save();
0

I suggest you try and hook on the order_cancel_after event and refund the ordered quantity using the event.

0

Your code just change order status. If you want to return item to stock you should use Mage_Sales_Model_Order::cancel() instead, or if payment method doesn't support cancelation try Mage_Sales_Model_Order::registerCancellation.

This method cancel all order items and return it to stock.

        foreach ($this->getAllItems() as $item) {
            ...
            $item->cancel();
        }

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.