I use sample-module-payment-gateway to test a payment gateway in magento 2, as i find out the order status is set to processing.

My question is how can i set this to pending, when a new order is placed?

I found a lot of tutorials about Magento-1. But not for Magento-2.

1 Answer 1


ok after a few hours of searching magento 2, I found an answer for this question.

public function setStatus($orderId="") {
    $orderModel = $this->_objectManager->create('\Magento\Sales\Model\Order'); 
    $order = $orderModel->load($orderId);
    $currentState = $order->getState();
    $currentStatus = $order->getStatus();

    $save = false;
    if ($currentState !== $orderModel::STATE_NEW) {
        $save = true;

    if ($currentStatus !== $orderModel::STATUS_FRAUD) {
        $save = true;

    if ($save) { $order->save(); }

This will set the payment sate to new and set the status to Pending.

can also use other status like

const STATE_PENDING_PAYMENT = 'pending_payment';

const STATE_PROCESSING = 'processing';

const STATE_COMPLETE = 'complete';

const STATE_CLOSED = 'closed';

const STATE_CANCELED = 'canceled';

const STATE_HOLDED = 'holded';

const STATE_PAYMENT_REVIEW = 'payment_review';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.