gpt4 book ai didi

php - 如果在 WooCommerce 中没有付款,X 天后自动取消订单

转载 作者:行者123 更新时间:2023-12-03 16:38:53 25 4
gpt4 key购买 nike

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

代码显然不完整,我正在寻求帮助以使其完整。我正在使用 -1 minute 对其进行测试看看有没有发生什么事。它没有。

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 中,there is already a function迷上了 woocommerce_cancel_unpaid_orders在 7 天后取消未付款订单的操作 Hook 。

我没找到woocommerce_cancel_unpaid_submitted Action 钩子(Hook),所以我不知道它是否存在以及是否被触发。

现在你的代码有一些错误,你可以更好地使用wc_get_orders()它直接为您提供WC_Order 的正确数组取而代之的是对象……

以下是一些不同的制作方法(最后一种未经测试):

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

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 文件中。

2) 这第三个解决方案 已经过测试和工作 :当任何订单变为“处理中”或“已完成”状态时触发该功能(每天仅执行一次):
// 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 文件中。

3) 所以你可以试试 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 文件中。

功能代码应该更好地工作。对于钩子(Hook)我真的不知道。

4) 你也可以试试 woocommerce_cancel_unpaid_orders Action Hook 。

关于php - 如果在 WooCommerce 中没有付款,X 天后自动取消订单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55792360/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com