Magento – Cannot cancel order on backend

magento-1.8order-statusorderspayment-gateway

I have created a custom payment gateway module. Everything works fine, except canceling the order from the backend.

The error: The order(s) cannot be canceled

Any ideas?

Best Answer

If you look into the function canCancel under Mage_Sales_Model_Order then you can see what in what situations an order can or cannot be cancelled.

The following need to be true before an order can be cancelled.

  1. If the payment can be voided,
  2. If you cannot unhold the order,
  3. If all items have have not been invoiced,
  4. If the order is not cancelled, complete or closed,
  5. Or if the order has not already been flagged to be cancelled,

The full code is as follows.

/**
 * Retrieve order cancel availability
 *
 * @return bool
 */
public function canCancel()
{
    if (!$this->_canVoidOrder()) {
        return false;
    }
    if ($this->canUnhold()) {  // $this->isPaymentReview()
        return false;
    }

    $allInvoiced = true;
    foreach ($this->getAllItems() as $item) {
        if ($item->getQtyToInvoice()) {
            $allInvoiced = false;
            break;
        }
    }
    if ($allInvoiced) {
        return false;
    }

    $state = $this->getState();
    if ($this->isCanceled() || $state === self::STATE_COMPLETE || $state === self::STATE_CLOSED) {
        return false;
    }

    if ($this->getActionFlag(self::ACTION_FLAG_CANCEL) === false) {
        return false;
    }
    /**
     * Use only state for availability detect
     */
    /*foreach ($this->getAllItems() as $item) {
        if ($item->getQtyToCancel()>0) {
            return true;
        }
    }
    return false;*/
    return true;
}
Related Topic