Mark a Magento order as complete programmatically

寵の児 提交于 2019-12-03 14:04:35
Roman Snitko

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();
Max

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();

I'm doing this that way:

$order->setState('complete', true, $this->__('Your Order History Message Here.'))
      ->save();

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();

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

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!