【问题标题】:Change WooCommerce order status to completed after a plugin is deactivated停用插件后将 WooCommerce 订单状态更改为已完成
【发布时间】:2021-05-01 01:52:14
【问题描述】:

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

作为额外的补充,它打算在插件停用后将当前状态为“已发货”的某些 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';
    }
}

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

【问题讨论】:

  • 哪个插件会被检查停用?

标签: php wordpress woocommerce plugins orders


【解决方案1】:

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

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

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

使用的功能:

  • deactivated_plugin() - 插件停用后触发。
  • wc_get_orders() 用于此目的,可以通过其他参数进一步扩展,例如某些订单状态、日期、customer_id 等。

【讨论】:

  • 谢谢你,这是工作,但很慢。我可以取消发送电子邮件完成通知吗?
  • 更改状态而不发送电子邮件通知。
  • 这确实取决于涉及多少现有订单。查看以下Unhook/remove WooCommerce Emails 或使用直接 SQL 查询将是更好的解决方案
猜你喜欢
  • 2022-10-14
  • 2017-05-24
  • 1970-01-01
  • 2019-06-01
  • 2016-04-17
  • 1970-01-01
  • 1970-01-01
  • 2020-02-19
  • 1970-01-01
相关资源
最近更新 更多