0

I need to change order status to custom status if payment fails.

I tried to get order via $payment->getOrder(), and tried to set state and status. I called save() also. However It looks like not working. It is always saving order with 'processing' status. However, It saves comments when I run following functions using same method; addStatusToHistory, addStatusHistoryComment. Any idea how can I implement this?

class Payment extends \Magento\Payment\Model\Method\Cc {
...
fn(){
$payment->getOrder()->setState( 'holded' )->setStatus( 'custom_new_status' )->save();

Thank you.

1 Answer 1

0

Try below code

        $state = 'holded';
        $status = 'custom_new_status';
        $comment = '';
        $order->setState($state);
        $order->setStatus($status);
        $order->addStatusToHistory($order->getStatus(), $comment);
        $order->save();

Reference

7
  • Hi, thanks for quick answer. I already tried, but It didn't work.
    – Mohamed
    Commented Jul 10, 2017 at 15:37
  • You can Use Event and Observer concept.Once payment has been completed observer can be called where you can set the order custom status @MmtBkn Commented Jul 11, 2017 at 4:19
  • Thanks @PranayK, I'm aware of it but thought should be easier way to do it.
    – Mohamed
    Commented Jul 11, 2017 at 16:40
  • @MmtBkn you should check your order object first. Commented Jul 11, 2017 at 16:43
  • @QaisarSatti there is order object. I can add comment to history, however something is overriding and changing status to processing
    – Mohamed
    Commented Jul 11, 2017 at 16:45

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.