How to Cancel an Order Programmatically with Custom Comments

modelorders

I know one can call $order->cancel() to easily cancel an order, but it calls registerCancellation() which in turns calls the internal _setState() itself.

The internal _setState() function will add a state change history all on its own, provided with no way to customize it.

If I want that status changing history contains my own comments, or even notify customers, how can I do that?

Best Answer

This should do the trick:

if ($order->canCancel()) {
    try {
        $order->cancel();

        // remove status history set in _setState
        $order->getStatusHistoryCollection(true);

        // do some more stuff here
        // ...

        $order->save();
    } catch (Exception $e) {
        Mage::logException($e);
    }
}
Related Topic