停用插件后将 WooCommerce 订单状态更改为已完成

Change WooCommerce order status to completed after a plugin is deactivated

我注册了一个发货订单状态,如果我停用插件,订单将被隐藏。

作为额外补充,我们打算在插件停用后将当前状态为“已发货”的某些 WooCommerce 订单状态更改为“已完成”

我试过这段代码,但它不起作用。可能是因为我应用不正确?

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';
    }
}

有人可以把我推向正确的方向吗?

要编辑现有订单的订单状态,当插件停用时,您可以使用:

注意:在此回答中,所有状态为“正在处理”的订单都更改为“已完成”。 根据需要进行调整。

// 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 );

使用的函数: