如果 WooCommerce 中没有付款,X 天后自动取消订单 [英] Automatically cancel order after X days if no payment in WooCommerce

查看:89
本文介绍了如果 WooCommerce 中没有付款,X 天后自动取消订单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网上搜索后设法把它放在一起,但它不起作用.我的目标是,如果订单在三天后仍未付款,则无论付款网关如何,自动取消所有处于暂停状态的订单.

I have managed to put this together after searching the web and yet, it does not work. My aim is to automatically cancel ALL orders with the status on-hold no matter the payment gateway if the order has not been paid after three days.

代码显然不完整,我正在寻求帮助以使其完整.我用 -1 分钟 测试它,看看是否发生了任何事情.没有.

The code is obviously incomplete and I'm asking for help making it complete. I was testing it with -1 minute to see if anything happened. It did not.

function get_unpaid_orders() {
    global $wpdb;

    $unpaid_orders = $wpdb->get_col( $wpdb->prepare( "
        SELECT posts.ID
        FROM {$wpdb->posts} AS posts
        WHERE posts.post_status = 'wc-on-hold'
        AND posts.post_date < %s
    ", date( 'Y-m-d H:i:s', strtotime('-1 minute') ) ) );

    return $unpaid_orders;
}

add_action( 'woocommerce_cancel_unpaid_submitted', 'cancel_unpaid_orders' );
function cancel_unpaid_orders() {
    $unpaid_orders = get_unpaid_orders();

    if ( $unpaid_orders ) {
        foreach ( $unpaid_orders as $unpaid_order ) {
            $order = wc_get_order( $unpaid_order );
            $cancel_order = true;

            foreach  ( $order->get_items() as $item_key => $item_values) {
                $manage_stock = get_post_meta( $item_values, '_manage_stock', true );
                if ( $manage_stock == "yes" ) {
                    $payment_method = $order->get_payment_method();
                    if ( $payment_method == "bacs" ) {
                        $cancel_order = false;
                    }
                }
            }
            if ( $cancel_order == true ) {
                $order -> update_status( 'cancelled', __( 'The order was cancelled due to no payment from customer.', 'woocommerce') );
            }
        }
    }
}

推荐答案

更新 4

注意:在 WooCommerce 中,已经有一个函数钩在 woocommerce_cancel_unpaid_orders 动作钩子中,可以在 7 天后取消未付款的订单.

Note: in WooCommerce, there is already a function hooked in woocommerce_cancel_unpaid_orders action hook that cancel unpaid orders after 7 days.

我没有找到woocommerce_cancel_unpaid_submitted动作钩子,所以我不知道它是否存在以及是否被触发.

I didn't find woocommerce_cancel_unpaid_submitted action hook, so I don't know if it exist and if it is triggered.

现在你的代码有一些错误,你可以更好地使用 wc_get_orders() 直接给你正确的 WC_Order 对象数组......

Now there is some mistakes in your code and you can better use wc_get_orders() which give you directly the correct array of WC_Order Objects instead…

这里有一些不同的制作方法(最后一个未经测试):

Here are some different ways to make it (last ones are untested):

1) 最后一个解决方案经过测试并有效当商店经理或管理员用户角色浏览管理员订单列表时(每天只执行一次):

1) This last solution is tested and works When shop manager or administrator user roles browse the Admin orders list (only executed once a day):

add_action( 'restrict_manage_posts', 'cancel_unpaid_orders' );
function cancel_unpaid_orders() {
    global $pagenow, $post_type;

    // Enable the process to be executed daily when browsing Admin order list 
    if( 'shop_order' === $post_type && 'edit.php' === $pagenow 
        && get_option( 'unpaid_orders_daily_process' ) < time() ) :

    $days_delay = 5; // <=== SET the delay (number of days to wait before cancelation)

    $one_day    = 24 * 60 * 60;
    $today      = strtotime( date('Y-m-d') );

    // Get unpaid orders (5 days old)
    $unpaid_orders = (array) wc_get_orders( array(
        'limit'        => -1,
        'status'       => 'on-hold',
        'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
    ) );

    if ( sizeof($unpaid_orders) > 0 ) {
        $cancelled_text = __("The order was cancelled due to no payment from customer.", "woocommerce");

        // Loop through orders
        foreach ( $unpaid_orders as $unpaid_order ) {
            $unpaid_order->update_status( 'cancelled', $cancelled_text );
        }
    }
    // Schedule the process to the next day (executed once restriction)
    update_option( 'unpaid_orders_daily_process', $today + $one_day );

    endif;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.

Code goes in function.php file of your active child theme (or active theme).

2) 第三种解决方案 经过测试并有效:当任何订单更改为正在处理"或已完成"状态时触发该功能(仅执行每天一次):

2) This third solution is tested and works: The function is triggered when any order change to "processing" or "completed" status (only executed once a day):

// Triggered on orders status change to "processing" or "completed"
add_action( 'woocommerce_order_status_changed', 'daily_cancel_unpaid_orders', 10, 4 );
function daily_cancel_unpaid_orders( $order_id, $old_status, $new_status, $order ) {
    // Enable the process to be executed daily
    if( in_array( $new_status, array('processing', 'completed') ) 
        && get_option( 'unpaid_orders_daily_process' ) < time() ) :

    $days_delay = 5; // <=== SET the delay (number of days to wait before cancelation)

    $one_day    = 24 * 60 * 60;
    $today      = strtotime( date('Y-m-d') );

    // Get unpaid orders (5 days old)
    $unpaid_orders = (array) wc_get_orders( array(
        'limit'        => -1,
        'status'       => 'on-hold',
        'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
    ) );

    if ( sizeof($unpaid_orders) > 0 ) {
        $cancelled_text = __("The order was cancelled due to no payment from customer.", "woocommerce");

        // Loop through WC_Order Objects
        foreach ( $unpaid_orders as $order ) {
            $order->update_status( 'cancelled', $cancelled_text );
        }
    }
    // Schedule the process to the next day (executed once restriction)
    update_option( 'unpaid_orders_daily_process', $today + $one_day );

    endif;
}

代码位于活动子主题(或活动主题)的 function.php 文件中.

Code goes in function.php file of your active child theme (or active theme).

3) 所以你可以试试 woocommerce_cancel_unpaid_submitted 动作钩子:

3) So you can try with woocommerce_cancel_unpaid_submitted action hook:

add_action( 'woocommerce_cancel_unpaid_submitted', 'cancel_unpaid_orders' );
function cancel_unpaid_orders() {
    $days_delay = 5; // <=== SET the delay (number of days to wait before cancelation)

    $one_day    = 24 * 60 * 60;
    $today      = strtotime( date('Y-m-d') );

    // Get unpaid orders (5 days old here)
    $unpaid_orders = (array) wc_get_orders( array(
        'limit'        => -1,
        'status'       => 'on-hold',
        'date_created' => '<' . ( $today - ($days_delay * $one_day) ),
    ) );

    if ( sizeof($unpaid_orders) > 0 ) {
        $cancelled_text = __("The order was cancelled due to no payment from customer.", "woocommerce");

        // Loop through orders
        foreach ( $unpaid_orders as $order ) {
            $order->update_status( 'cancelled', $cancelled_text );
        }
    }
}

代码位于活动子主题(或活动主题)的 function.php 文件中.

Code goes in function.php file of your active child theme (or active theme).

函数代码应该更好地工作.对于钩子,我真的不知道.

The function code should better works. For the hook I really don't know.

4) 您也可以尝试使用 woocommerce_cancel_unpaid_orders 操作挂钩代替.

4) You can try also with woocommerce_cancel_unpaid_orders action hook instead.

这篇关于如果 WooCommerce 中没有付款,X 天后自动取消订单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆