简体   繁体   English

自动更改 Woocommerce 订单状态失败 -> 取消

[英]Auto change Woocommerce order status Failed -> Canceled

I am trying to automatically change Woocommerce orders with "Failed" status to "Cancel" status after 24h.我试图在 24 小时后自动将状态为“失败”的 Woocommerce 订单更改为“取消”状态。 It means the customer has not been able to pay his order.这意味着客户无法支付他的订单。

I have tried many things so far but was not able to make it work.到目前为止,我已经尝试了很多东西,但无法让它发挥作用。 Here is the mu-plugin I have created :这是我创建的 mu 插件:

    <?php
add_action( 'woocommerce_cancel_unpaid_submitted', 'cancel_failed_orders' );
function cancel_failed_orders() {
    $days_delay = 1; 
    $one_day    = 24 * 60 * 60;
    $today      = strtotime( date('Y-m-d') );
    $failed_orders = (array) wc_get_orders( array(
        'limit'        => -1,
        'status'       => 'wc-failed',
        'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
    ) );
    if ( sizeof($failed_orders) > 0 ) {
        $cancelled_text = __("No successful payment", "woocommerce");
        foreach ( $failed_orders as $order ) {
            $order->update_status( 'wc-cancelled', $cancelled_text );
        }
    }
}

Does anyone has any idea what I am missing ?有谁知道我错过了什么?

It looks like "woocommerce_cancel_unpaid_submitted" action doesn't exists.看起来“woocommerce_cancel_unpaid_submitted”操作不存在。 You can use "woocommerce_cancel_unpaid_orders" action.您可以使用“woocommerce_cancel_unpaid_orders”操作。

Here is a solution with wp-cron job.这是 wp-cron 作业的解决方案。 To test it at full install WP Crontrol plugin so you can manualy run the cron job when you need it or wait 1 hour.要在完全安装 WP Crontrol 插件时对其进行测试,以便您可以在需要时手动运行 cron 作业或等待 1 小时。 Your cron job should be listed as name failed_orders_event .您的 cron 作业应列为名称 failed_orders_event 。

Disable wp-cron and use server cron if you want execution on every hour with or without visitors.如果您希望在有或没有访问者的情况下每小时执行一次,请禁用 wp-cron 并使用服务器 cron。 There is alot of info on the web.网上有很多信息。

add_action('failed_orders_event', 'check_failed_orders_every_hour');
// The action will trigger when someone visits your WordPress site
function failed_orders_event_activation() {
    if ( !wp_next_scheduled( 'failed_orders_event' ) ) {
        wp_schedule_event( time(), 'hourly', 'failed_orders_event');
    }
}
add_action('wp', 'failed_orders_event_activation');

function check_failed_orders_every_hour() {
    $failed_orders = wc_get_orders( array(
        'limit'        => -1,
        'status'       => 'failed',
    ));

    foreach ( $failed_orders as $order ) {
        $cancelled_text = __("No successful payment", "woocommerce");
        $order->update_status( 'cancelled',$cancelled_text);
    }
    
}

i need the same but backwards我需要相同但倒退

WooCommerce "Cancelled" Orders change to "failed" status WooCommerce“取消”订单更改为“失败”状态

WooCommerce cancels an abandoned order 24 hours after not receiving payment, the order ON HOLD status automatically goes to CANCELED, so far so good, now when the order changes CANCELLED, I need them to change to "failed" 1 hour later. WooCommerce 在未收到付款 24 小时后取消了一个废弃的订单,订单 ON HOLD 状态自动变为 CANCELLED,到目前为止一切顺利,现在当订单更改为 CANCELLED 时,我需要他们在 1 小时后更改为“失败”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM