You should be able to hook into transition_post_status
action to add custom code before the post status of an appointment post type is changed.
In the example below I am checking if the post_type of changed post is ‘appointment‘ (please replace it with your post status slug). Then I am trying to get WC_Order object (assuming the linked order ID is stored as a post meta linked_order_id
for each appointment, you didn’t specify this in your question). If the order is found I am setting the order status based on the post status of the appointment.
function update_order_status_by_appointment( $new_status, $old_status, $post ) {
// Perform only for 'appointment' post type
if( get_post_type( $post ) === 'appointment' ) {
// Get WC_Order linked to this appointment
$order_id = get_post_meta( $post->ID, 'linked_order_id', true);
$order = wc_get_order( $order_id );
// Check if order with this ID exists
if( $order ) {
// If appointment is being changed to 'draft', change order status to 'cancelled'
if( $new_status === 'draft' ) {
$order->update_status('cancelled');
}
// Add other conditions as you wish
// ...
}
}
}
add_action('transition_post_status', 'update_order_status_by_appointment', 10, 3);
Code goes in functions.php of your active child-theme or theme. It is not tested though, but it should work.