Magento 2:如何在自定义模块中以编程方式更新订单状态?


11
$orderId = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId); 
$order->setState("processing")->setStatus("processing");
$order->save();

请告诉我如何更新Magento 2中的订单状态?

Answers:


19

您可以按照以下方式进行操作

在名称空间下面声明

use Magento\Sales\Model\Order;

$orderId = 1;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('\Magento\Sales\Model\Order') ->load($orderId);
$orderState = Order::STATE_PROCESSING;
$order->setState($orderState)->setStatus(Order::STATE_PROCESSING);
$order->save();

希望这可以帮助


感谢您的回复@Dhaval Drcsystems我只希望更新订单状态
Rahul Katoch

您可以参考此模型,以便更好地了解Magento \ Sales \ Model \ Order \ Payment在此处检查他们如何使用代码处理订单。
Dhaval Solanki

上面的代码显示\ Magento \ Sales \ Model \ Order not found错误
Jaisa

你能告诉我你的档案吗?
Dhaval Solanki

如何更新自定义订单状态?我的状态是否为“ sent_to_print”?
贾法尔·品哈尔(Jafar Pinjar)

12

订单状态更新

为了以编程方式更新订单状态和状态,以便以这种格式定义状态和状态。在构造函数中初始化订单对象,并在自定义函数中使用该订单对象对其进行更新。要以编程方式在模型中更新订单状态,请从构造函数中获取订单对象。

public function __construct(
    \Magento\Sales\Model\Order $order
){
    $this->order = $order;
}
public function updateorder(){
    $order = $this->order;
    $order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING, true);
    $order->setStatus(\Magento\Sales\Model\Order::STATE_PROCESSING);
    $order->addStatusToHistory($order->getStatus(), 'Order processed successfully with reference');
    $order->save();
}

新订单电子邮件

$orderid = '10000000';
$order = $this->_objectManager->get('Magento\Sales\Model\Order')->loadByIncrementId($orderid);
$this->_objectManager->get('Magento\Sales\Model\Order\Email\Sender\OrderSender')->send($order);

非常感谢@calypso的回复,但我只想更新订单状态
Rahul Katoch

@Rahulocodewire也有评论。
gelanivishal

@ calypso,请解释一下我将此代码放在模块中的什么位置
Rahul Katoch

我如何显示控制器中的待处理状态?
拉胡尔·卡托奇

5

由于不推荐使用load()save()(因为:)

* @deprecated 100.1.0 because entities must not be responsible for their own loading.
* Service contracts should persist entities. Use resource model "load" or collections to implement
* service contract model loading operations.

我使用OrderRepositoryInterface加载和保存订单:

use Magento\Sales\Api\OrderRepositoryInterface;

public function __construct(
    OrderRepositoryInterface $orderRepository,
    ...
) {
    $this->orderRepository = $orderRepository;
}

...
$order = $this->orderRepository->get($orderId);
$order->setState(\Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW);
$order->setStatus(\Magento\Sales\Model\Order::STATE_PAYMENT_REVIEW);

try {
    $this->orderRepository->save($order);
} catch (\Exception $e) {
    $this->logger->error($e);
    $this->messageManager->addExceptionMessage($e, $e->getMessage());
}

希望能帮助到你


如何更新自定义订单状态?
Jafar Pinjar

3

感谢@medmek和@ prince-patel的回答。作为来自@ jafar-pinjar的有关自定义订单状态的问题,setState和setStatus调用可以采用状态代码。例如,创建自定义状态代码“已付款”。要将状态更新为订单:

...
use \Magento\Sales\Api\OrderRepositoryInterface;

class nameOfTheClass {
    ...
    protected $_orderRepository;
    ...
    public function __construct(..., OrderRepositoryInterface $orderRepository, ...){

        $this->_orderRepository = $orderRepository;
        ...
    }
    ...
    public function setOrderStatus($orderID, $statusCode){
        try{
            // obtain the order with the order ID
            $order = $this->_orderRepository->get($orderID);
            $order->setState($statusCode)->setStatus($statusCode);
            $this->_orderRepository->save($order);
            return true;
        } catch (\Exception $e){
            // add some logging here
            return false;
        }
    }
    ...
}

要更新订单状态:

$orderID = 1234; // this is the order ID
$code = 'paid';
$this->setOrderStatus($orderID, $code);

希望可以帮助某人。


1
增量ID类似于#3000001469-1,而不是1234。1234而是订单ID
Greck 19'Mar

感谢您指出@Greck。更新了响应。
qgor

2

Magento 2.2.2这仅适用于以下方式!

    $order = $this->order->loadByIncrementId('000000001');
    //$order = $this->order;
    $order->setState(\Magento\Sales\Model\Order::STATE_PROCESSING, true)->save();
    $order->setStatus(\Magento\Sales\Model\Order::STATE_PROCESSING, true)->save();
    $order->addStatusToHistory($order->getStatus(), 'Order processed successfully with reference again and again');
    //$order->save();

但是我唯一的问题是它不能完全更新状态: 在此处输入图片说明

它只会更新网格中的和以下内容: 在此处输入图片说明

也无法创建新的发票,运输和贷项凭证。谢谢

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.