Pregunta

In some (not all) cases, I want to set the status of an order to a new custom status after it has been shipped.

Where in the core code is an order updated to complete once it has been invoiced and shipped?

¿Fue útil?

Solución 2

It turns out using an observer wasn't the optimal solution - another process further down the chain was setting the status back to complete. I also didn't feel that overwriting a previously set status was the most elegant way to achieve what I needed.

I was able to find the method responsible for changing the status of an order after it's set to both shipped and invoiced:

\Magento\Sales\Model\ResourceModel\Order\Handler\State::check($order)

This is called whenever an order's status changes (and in all likelihood is what was reverting the status change from my observer)

By creating an after plugin, I was able to change the status as part of the normal order workflow.

public function afterCheck($subject, $result, $order)
{
  if ($my_logic) {
    $order->setState(Order::STATE_PROCESSING)
      ->setStatus('my_status');
    }
}

Otros consejos

You can create a new order status by going to Stores > Configuration > Order Status, and create a new status for that and make sure it is enabled and you'll be able to see your custom generated status under order edit settings.

enter image description here

Now to set your order to custom status whenever it is marked as shipped, you can put an observer event sales_order_save_after, which will execute whenever there is a change in order data, so you need to check the order status in Observer event and if it is changed to shipped code you can override it into your observer execution to your custom status code programmatically.

I hope this will help you. If not, feel free to comment.

It's better to create it via the admin panel by going to Stores > Configuration > Order Status, and ensure to create a new status for it. Don't forget to enable it. I Hope, with this you can see your custom generated status under order edit settings.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a magento.stackexchange
scroll top