2

I'm using the code below for change order status to holded

$order->setState("holded")->setStatus("holded");
$order->save();

But after run this code, the status of order is blank, what am i wrong ? enter image description here

1

You code seems well. But ensure that holded is the default status of order state holded.

Using $order->setState("holded")->setStatus("holded"); means automatically not changes the order status to hold.

before changes an order status to hold, you have to check the order is available for the hold by canHold().

if ($order->canHold()) {
    $order->setState("holded")->setStatus("holded");
    $order->save();
}

If you want do this at a class then use below

 protected $orderManagement;
public  function __construct(
    \Magento\Sales\Api\OrderManagementInterface $orderManagement
)
{
    $this->orderManagement = $orderManagement;
}
public function canChangetoHold()
{

    try {
        $this->orderManagement->hold(10); //10 is order id

    } catch (\Magento\Framework\Exception\LocalizedException $e) {
        // some error
        $e->getMessage();
    } catch (\Exception $e) {
        //You have not put the order on hold
    }
}
  • I also found code on the file : vendor/magento/module-sales/Controller/Adminhtml/Order/Hold.php – Vu Tran Kien Aug 21 '18 at 6:43
  • YEs, you will get code at there. – Amit Bera Aug 21 '18 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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