1

I have created a custom status called "custom_canceled" and associated it with the "canceled" state. I also have another status called "Canceled" associated with the state "canceled", this is the one used by default when an order is canceled.

I need that when canceling an order that has been made using the payment method "banktransfer" the order status will be "custom_canceled" and not "canceled" (default status) but I have no idea how I can do this.

I am working on Magento 2.3.5-p1. I appreciate any information to guide me.

0

You can use event order_cancel_after and change status this way:

$order = $observer->getEvent()->getData('order');
$order->setStatus('your_custom_status_code');

Then order will be saved by the OrderService in the cancel method:

public function cancel($id)
{
    $order = $this->orderRepository->get($id);
    if ($order->canCancel()) {
        $order->cancel(); // <<< Inside this method the event fired
        $this->orderRepository->save($order); // Magento saves order with new custom status
        return true;
    }

    return false;
}

The payment method you can check inside observer using $order->getPaymentMethod(), it will return string (code).

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.