Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mark a Magento order as complete programmatically

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.

like image 576
gregdev Avatar asked Jan 18 '12 07:01

gregdev


3 Answers

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();
like image 142
Max Avatar answered Sep 24 '22 02:09

Max


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();
like image 40
Roman Snitko Avatar answered Sep 22 '22 02:09

Roman Snitko


I'm doing this that way:

$order->setState('complete', true, $this->__('Your Order History Message Here.'))
      ->save();
like image 24
user487772 Avatar answered Sep 22 '22 02:09

user487772