有没有办法以编程方式在 Magento 中重新打开已达到完成或关闭状态的订单?我有以下代码可用于更改订单状态,但我无法让它为完成或关闭的订单工作。
// connect to magento
require_once('app/Mage.php');
umask(022);
Mage::app();
// check admin credentials
Mage::getSingleton('core/session', array('name' => 'adminhtml'));
$admin = Mage::getSingleton('admin/session');
if ( $admin->isLoggedIn() ) {
// update order status
$orderIncrementId = "100000001";
$order = Mage::getModel('sales/order')->loadByIncrementId($orderIncrementId);
$order->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save();
}
我得到了当前代码 here .在那个页面上它说它是用 Magento 1.3.2.4 测试的,但我使用的是 Magento 1.6.x。也许这就是问题所在?
如果我需要提供更多详细信息,请告诉我,感谢您提供的任何帮助。
最佳答案
我认为您这里没有 Magento 版本问题。
在特定情况下,Magento 只是不允许将订单状态切换回 Mage_Sales_Model_Order::STATE_PROCESSING
。
例如,通常您不能将 Mage_Sales_Model_Order::STATE_PROCESSING
状态保存到任何已经有退款 (creditmemos) 的订单。既不在 1.3.2.4 中,也不在 1.6.x 中。
这是设计使然。
查看 Mage_Sales_Model_Order::_checkState()
,了解 Magento 在何种情况下强制将订单状态分别重置为 STATE_COMPLETE
或 STATE_CLOSED
。
protected function _checkState()
{
if (!$this->getId()) {
return $this;
}
$userNotification = $this->hasCustomerNoteNotify() ? $this->getCustomerNoteNotify() : null;
if (!$this->isCanceled()
&& !$this->canUnhold()
&& !$this->canInvoice()
&& !$this->canShip()) {
if (0 == $this->getBaseGrandTotal() || $this->canCreditmemo()) {
if ($this->getState() !== self::STATE_COMPLETE) {
$this->_setState(self::STATE_COMPLETE, true, '', $userNotification);
}
}
/**
* Order can be closed just in case when we have refunded amount.
* In case of "0" grand total order checking ForcedCanCreditmemo flag
*/
elseif (floatval($this->getTotalRefunded()) || (!$this->getTotalRefunded()
&& $this->hasForcedCanCreditmemo())
) {
if ($this->getState() !== self::STATE_CLOSED) {
$this->_setState(self::STATE_CLOSED, true, '', $userNotification);
}
}
}
if ($this->getState() == self::STATE_NEW && $this->getIsInProcess()) {
$this->setState(self::STATE_PROCESSING, true, '', $userNotification);
}
return $this;
}
要回答您的问题:您可以通过使用您自己的方法覆盖 _checkState()
方法来实现您想要做的事情,该方法允许设置 STATE_PROCESSING
。
请注意,这很可能会导致创建 Magento 既不知道、也不期望或无法处理的新状态上下文。
如果您的更改造成严重破坏,请不要怪我。你已经被警告了^^
关于magento - 在 Magento 中重新打开一个完整/关闭的订单?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8335227/