Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change order status when refund in magento?

I am working on Magento 1.7 version. I placed a order and make payment using Paypal and refund the amount offline. Order status changed following:-

  1. Pending Payment
  2. Invoice #100000001 created
  3. Processing (IPN "Completed".Registered notification about captured amount of £1. Transaction ID: "0CT123456789874521". )
  4. Processing (Notified customer about invoice #100000001. )
  5. Credit memo #100000001 created
  6. Processing (Refunded amount of £1 offline. )
  7. Processing (IPN "Refunded". Note: Maximum amount available to refund is £0.00 )
  8. Processing (Test order has been refunded.)

Order Status is still showing processing but it should be completed.

like image 795
Neeraj Garg Avatar asked Apr 30 '14 09:04

Neeraj Garg


3 Answers

In Magento, an order is only marked as Complete once you create an invoice and shipment for it. When you create a credit memo for an order, it instead would be marked as Closed.

If you try to set an order as complete or closed directly using the setStatus method, you will get an exception: The Order State "complete" must not be set manually. Again, these states should be set automatically by Magento.

That being said, if you really want to set these manually, you can get around it like so:

$order->setData('state', 'complete');
$order->setStatus('complete');
$history = $order->addStatusHistoryComment('Manually set order to Complete.', false);
$history->setIsCustomerNotified(false);
$order->save();

You can have a look at this stackoverflow thread for some more info.

like image 129
Cristian Quiroz Avatar answered Oct 11 '22 10:10

Cristian Quiroz


I've been investigating this issue and it seems that it is actually a rounding problem. After creating a credit memo the order status should be closed but in my case also some refunded orders kept their original status.

When creating a credit memo two functions Mage_Sales_Model_Order::canCreditmemo() and Mage_Sales_Model_Order_Invoice::canRefund() are called. They both return false if the difference between grand total and refunded amount is less than 0.0001.

In my testing this wasn't the case for some refunded orders irrespective to the used payment method. Increasing the value to 0.001 in both functions resulted in a closed order status. This also explains why only some orders kept their state and some are closed correctly depending on price and tax amount.

I solved the issue by overriding both Magento core classes in local and replaced the following lines with this:

Mage_Sales_Model_Order:

if (abs($this->getStore()->roundPrice($this->getTotalPaid()) - $this->getTotalRefunded()) < .001) {
    return false;
}

Mage_Sales_Model_Order_Invoice:

if (abs($this->getBaseGrandTotal() - $this->getBaseTotalRefunded()) < .001) {
    return false;
}

I hope this helps others because it took me some time to trace down that bug.

like image 35
Jan Knipper Avatar answered Oct 11 '22 12:10

Jan Knipper


Have a look at Mage_Sales_Model_Order_Payment::refund() where the refund is processed:

    $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true, $message);
    Mage::dispatchEvent('sales_order_payment_refund', array('payment' => $this, 'creditmemo' => $creditmemo));

After the order state was set to processing the event sales_order_payment_refund is dispatched. You could write an observer which listens to this event. If the payment method was PayPal you could update the order state as described in the answer by Cristian Quiroz.

like image 32
Simon H Avatar answered Oct 11 '22 12:10

Simon H