php 以编程方式将 Magento 订单标记为完成

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8906688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 05:46:12  来源:igfitidea点击:

Mark a Magento order as complete programmatically

phpmagentomagento-1.4

提问by gregdev

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:

我正在生成发票(我认为我不需要这个,因为每个项目在 Magento 后端都被标记为“已开票”)和如下发货:

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

任何提示将非常感谢。

采纳答案by 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();

回答by 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)

此方法在app/code/local/Mage/Sales/Model/Order.php(在 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:

在 Magento 1.7.0.0 中,此方法已被删除。试试这个:

    $order->setData('state', "complete");
    $order->setStatus("complete");
    $history = $order->addStatusHistoryComment('Order marked as complete automatically.', false);
    $history->setIsCustomerNotified(false);
    $order->save();

回答by user487772

I'm doing this that way:

我是这样做的:

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

回答by Ahmad Vaqas Khan

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

以编程方式处理订单的代码。可以放在成功事件或 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();

回答by Jongosi

Magento will automatically mark an order as complete if:

在以下情况下,Magento 会自动将订单标记为完成:

  • 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

应该可以正常工作。在 Magento 1.7.0.2 中测试

回答by espradley

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.

System->Order Status 创建一个名为 Completed 的新状态(注意 d,这样很容易区分)将该状态分配给状态 Processing/pending,无论如何。

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

这对我们的客户有效——但如果您严重依赖订单状态(不同于订单状态),则行不通。