There are events for gaining an order +event responses as well as a function to get a unit's current order.
So on gain event, save the issued order, then periodically check if the unit's order is reset, reset the saved order too. Furthermore, the gain event can also be the point in time a previous order is overwritten, therefore check the previously saved order. Exceptions are instant parallel orders like learning an ability since those do not cancel anything.