Mark a Magento order as complete programmatically
Asked Answered
S

6

8

I'm trying to mark a "Processing" order as Complete when I get a certain response back from a third party service. I've got everything set up for this, but the only problem is that orders are staying in the Processing state.

I'm generating an invoice (I don't think I need this though, as each item is marked as "invoiced" in the Magento backend) and a shipment like so:

$order = Mage::getModel('sales/order')... (etc)
$shipment = $order->prepareShipment($quantities);
$shipment->register();
$shipment->setOrder($order);
$shipment->save();

$invoice = $order->prepareInvoice($quantities);
$invoice->register();
$invoice->setOrder($order);
$invoice->save();

This doesn't seem to be doing it though - I get no errors back from this code, but the order remains as processing. In the backend I can still see the "Ship" button at the top of the order, and each item is in the "invoiced" state.

Any tips would be greatly appreciated.

Serosa answered 18/1, 2012 at 7:35 Comment(0)
N
5

You can take a look at this article (in Russian).

Here is the code from the article:

$order = $observer->getEvent()->getOrder();

if (!$order->getId()) {
    return false;
}

if (!$order->canInvoice()) {
    return false;
}

$savedQtys = array();
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($savedQtys);
if (!$invoice->getTotalQty()) {
    return false;
}
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
$invoice->register();

$invoice->getOrder()->setCustomerNoteNotify(false);
$invoice->getOrder()->setIsInProcess(true);

$transactionSave = Mage::getModel('core/resource_transaction')
    ->addObject($invoice)
    ->addObject($invoice->getOrder());

$transactionSave->save();
Nunhood answered 18/1, 2012 at 8:30 Comment(5)
Thanks, the code in the article did the trick after I changed to to a shipment rather than an invoice.Serosa
Unfortunately, the link is now broken.Transit
Would be really nice to have an updated link. archive.org still has a version of the link in case anyone is interested. web.archive.org/web/20110414102634/http://snowcore.net/…. Use google chrome to get a translation.Stouthearted
@Transit domain will be available in few days, so you can check laterNunhood
@Serosa can you share your solution? (shipment rather than invoice)Biscuit
C
17

Try

$order->setStateUnprotected('complete',
    'complete',
    'Order marked as complete automatically',
    false);

This method is in app/code/local/Mage/Sales/Model/Order.php (in v1.6.1)

938:    public function setStateUnprotected($state, $status = false, $comment = '', $isCustomerNotified = null)

In Magento 1.7.0.0 this method has been removed. Try this instead:

    $order->setData('state', "complete");
    $order->setStatus("complete");
    $history = $order->addStatusHistoryComment('Order marked as complete automatically.', false);
    $history->setIsCustomerNotified(false);
    $order->save();
Conservator answered 18/1, 2012 at 7:45 Comment(2)
Thanks for the response. This didn't seem to have any effect in Magento 1.4Serosa
Threw error, Call to a member function getMethodInstance() on a non-object in 1.7.0.2Edgar
N
5

You can take a look at this article (in Russian).

Here is the code from the article:

$order = $observer->getEvent()->getOrder();

if (!$order->getId()) {
    return false;
}

if (!$order->canInvoice()) {
    return false;
}

$savedQtys = array();
$invoice = Mage::getModel('sales/service_order', $order)->prepareInvoice($savedQtys);
if (!$invoice->getTotalQty()) {
    return false;
}
$invoice->setRequestedCaptureCase(Mage_Sales_Model_Order_Invoice::CAPTURE_OFFLINE);
$invoice->register();

$invoice->getOrder()->setCustomerNoteNotify(false);
$invoice->getOrder()->setIsInProcess(true);

$transactionSave = Mage::getModel('core/resource_transaction')
    ->addObject($invoice)
    ->addObject($invoice->getOrder());

$transactionSave->save();
Nunhood answered 18/1, 2012 at 8:30 Comment(5)
Thanks, the code in the article did the trick after I changed to to a shipment rather than an invoice.Serosa
Unfortunately, the link is now broken.Transit
Would be really nice to have an updated link. archive.org still has a version of the link in case anyone is interested. web.archive.org/web/20110414102634/http://snowcore.net/…. Use google chrome to get a translation.Stouthearted
@Transit domain will be available in few days, so you can check laterNunhood
@Serosa can you share your solution? (shipment rather than invoice)Biscuit
P
3

I'm doing this that way:

$order->setState('complete', true, $this->__('Your Order History Message Here.'))
      ->save();
Pneumonectomy answered 18/1, 2012 at 8:37 Comment(1)
Thanks for the response. This brought up the error "The Order State "complete" must not be set manually."Serosa
W
1

Code for processing order programmatically. Can be put on success event or cron

$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);

$order->setData('state', Mage_Sales_Model_Order::STATE_COMPLETE);
$order->setStatus(Mage_Sales_Model_Order::STATE_COMPLETE);

$history = $order->addStatusHistoryComment('Order is complete', false);
$history->setIsCustomerNotified(false);

$order->save();
Walley answered 6/2, 2017 at 10:30 Comment(3)
Why are you setting state and status to the same value?Tharpe
@MarcusWolschon : For better understanding take a look of "sales_order_status" table . Status is a representation of state and based on it , status label is displayed.Walley
The names of the shop-specific status codes (and their translated labels) are usually different from the fixed Magento state values. A status is assigned to a state and one status may be assinged as the default status for a Magento state. So the question remains. Why set status and state to the same value. The status is Shop-specific and should not come from a Magento constant for a state.Tharpe
B
0

Magento will automatically mark an order as complete if:

  • Payment has been made.
  • An invoice exists.
  • A shipment exists.

If you cannot do that, try to create a custom 'state' and set that. In the meantime, to set the order to processing, try this:

 $order = Mage::getModel('sales/order')->load($id);
 $order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();

Should work without errors. Tested in Magento 1.7.0.2

Brookner answered 12/2, 2013 at 1:41 Comment(1)
But you can not create a custom state. Just a custom status and assign one or multiple status values to a state.Tharpe
M
0

In my case, I needed the end users to see completed in the order grid, but the order state really made no difference. So I did just went to

System->Order Status Create a new Status called Completed (note the d so it's easy to differentiate) Assign that status to the state Processing/pending, whatever.

This worked for our client -- but wouldn't work if you heavily depend on order state (Different than order status).

Milka answered 25/6, 2015 at 1:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.