Order Status – How to Set Order Status to Closed

order-statusrefund

I am programatically doing an invoice refund, and I want to set the order status to closed after the refund has happened.

The refund works, but changing the order status doesn't.

I was kind of hoping that the refund code would set the status to Closed for me, but it doesn't, the status just stays at Processing after the refund.

Here's the refund code:

$service = Mage::getModel('sales/service_order', $order);
$creditmemo = $service->prepareInvoiceCreditmemo($invoice);
$creditmemo->register();
$creditmemo->save();

I have read that the Complete and Closed statuses shouldn't be set manually, but I do need the order status changed to closed.

Here's what I've tried, none worked for me.

1.

$order->addStatusHistoryComment("Refunded.", Mage_Sales_Model_Order::STATE_CLOSED);

2.

$order->setData('state', Mage_Sales_Model_Order::STATE_CLOSED);
$order->save();

3.

$order->setState(Mage_Sales_Model_Order::STATE_CLOSED)->save();

Best Answer

Order status/state Closed/closed is a "protected" status/state, which cannot be changed, along with Complete/complete. This means when you use the method setState, it will not work.

This is because in order to have a Closed order, you must have a fully invoiced and shipped order fully refunded. It looks like in your case you don't have a full shipment. If you have a full shipment and still not getting to Closed, something was done wrong.

In any case, you have two options.

  1. Do a full shipment (dummy if necessary). Refund fully and you should get to Closed automatically.

  2. Go around the restriction by using setData('state', Mage_Sales_Model_Order::STATE_CLOSED) instead of setState.

Related Topic