1
votes

I registred a shipped order status, if I deactivated the plugin, the order will be hidden.

As an extra addition, it is the intention to change certain WooCommerce order statuses that currently have a status of "shipped" to "completed" after the plugin is deactivated

I tried this code but it doesn't work. Probably because I am applying this incorrectly?

add_action( 'woocommerce_order_status_changed', 'deactivate_plugin_conditional', 10, 2 );
function deactivate_plugin_conditional( $order_status, $order ) {
    if( $order->has_status( 'shipped' ) ) {
        return 'Complated';
    }
}

Can someone push me in the right direction?

1
Which plugin will be checked for deactivation? - WordPress Mechanic

1 Answers

0
votes

To edit the order status of existing orders, when a plugin is deactivated, you can use:

Note: in this answer, all orders with the status "processing" are changed to "completed". Adjust as needed.

// Fires after a plugin is deactivated.
function action_deactivated_plugin( $plugin, $network_activation ) {
    // Path to the plugin file relative to the plugins directory.
    if ( $plugin == 'my-plugin/my-plugin.php' ) {
        
        // Get ALL orders with certain status.
        // NOTE THE USE OF WC-.. at the order status
        $orders = wc_get_orders( array(
            'status' => array( 'wc-processing' ),
        ));
        
        // NOT empty
        if ( sizeof( $orders ) > 0 ) {
            // Iterating through each order with certain status
            foreach ( $orders as $order ) {
                // Change order status
                $order->update_status( 'completed' );
            }
        }
    }   
}
add_action( 'deactivated_plugin', 'action_deactivated_plugin', 10, 2 );

Functions used:

  • deactivated_plugin() - Fires after a plugin is deactivated.
  • wc_get_orders() is used for this, this can be further expanded with additional parameters such as certain order statuses, date, customer_id, etc.