在搜索网络后,我设法将其放在一起,但是它不起作用。我的目标是,如果三天后仍未付款,则无论付款网关如何,都将自动取消所有处于保留状态的订单。
代码显然不完整,我正在寻求帮助以使其完整。我正在用String query = "ALTER TABLE audit ALTER COLUMN event TYPE character varying(3000)";
String command[] = new String[] { "psql", "--dbname=postgresql://postgres:password@127.0.0.1:1234/db123" };
Exec exec = new Exec();
Process exec2 = exec.exec(namespace, podName, command, dbContainerName, query, stdOut);
Thread.sleep(10000);
exec2.destroy();
测试它,看是否发生了任何事情。没有。
-1 minute
答案 0 :(得分:2)
更新4
注意:在WooCommerce中,there is already a function插入了woocommerce_cancel_unpaid_orders
动作挂钩,可在7天后取消未付款订单。
我没有找到woocommerce_cancel_unpaid_submitted
动作挂钩,所以我不知道它是否存在以及是否被触发。
现在您的代码中存在一些错误,您最好使用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 $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文件中。
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 $unpaid_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
动作挂钩:
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文件中。
功能代码应该更好地工作。对于钩子,我真的不知道。
4)您也可以尝试使用woocommerce_cancel_unpaid_orders
动作挂钩。
答案 1 :(得分:0)
在创建订单后的4小时内,我需要在某些情况下取消保留的订单。
示例:2个客户
第一位客户在下午5:00发出订单,因此其保留状态(我希望订单创建后的4个小时后,如果他不付款,则取消订单,因此必须在9点取消: 00 PM)
第二个客户在5:37下订单,因此其保留状态(我也希望从订单创建之日起4小时后取消订单,如果他不付款,则必须在9时取消): 37)
我希望它在每个订单分别从订单创建日期起4小时后过期后立即取消订单,而不是作为cron作业每4小时工作一次,因此在上一个示例中,如果cron事件从6开始:下午00点,每4小时运行一次,因此第一个客户订单将在10:00 PM过期,第二个客户订单也将在10:00 PM过期。
我已经尝试了以上解决方案
,但是无法使它在仅按天数定义的4小时后过期,也不知道哪个答案对我来说是最佳方案,对于创建后4小时后要分别取消的每个订单日期不是cron事件,
我使用了上面相关问题中提到的许多解决方案,但是只有当第一个答案起作用时,我才记得这是当管理员/商店经理访问订单页面时..我需要自动完成操作而无需人工干预
此第三种解决方案已经过测试,并且可以正常工作:但是我希望在订单处于保留状态时未完成/正在处理的情况下将其取消,如果希望在订单创建日期之后的4个小时过去,则取消该订单。计划并每30分钟(而不是每天)运行
答案 2 :(得分:0)
只是添加到@LoicTheAztec 的精彩回答中。我将其修改为插件,并添加了一个设置页面,您可以在其中启用/禁用此功能,以及更改管理设置中的延迟天数。代码如下:
<?php
/**
* Plugin Name: Cancel On-Hold Orders After X Days
* Plugin URI: https://smartairfilters.com
* Description: Cancel orders that are in an 'on-hold' status after a certain number of days. Uses wordpress cron to trigger
* Version: 1.0.0
* Author: Paddy Robertson
* Author URI: https://patrickrobertson.uk
* Requires at least: 5.3
* Requires PHP: 7.0
*/
add_action('woocommerce_cancel_unpaid_orders', 'cancel_onhold_orders');
function cancel_onhold_orders() {
if (!get_option('cancel_onhold_enable')) {
// only run if feature is enabled
return;
}
$days_delay = get_option('cancel_onhold_days_delay'); // <=== SET the delay (number of days to wait before cancelation)
echo "days delay xxxx " . $days_delay;
$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(
'orderby' => 'date',
'order' => 'DESC',
'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 );
}
}
}
// Settings page
function cancel_onhold_register_settings() {
register_setting( 'cancel_onhold_group', 'cancel_onhold_days_delay');
register_setting( 'cancel_onhold_group', 'cancel_onhold_enable');
}
add_action( 'admin_init', 'cancel_onhold_register_settings' );
function cancel_onhold_register_options_page() {
add_options_page('Cancel On-Hold Orders', 'Cancel On-Hold', 'manage_options', 'cancel_onhold_options_page', 'cancel_onhold_options_page');
}
add_action('admin_menu', 'cancel_onhold_register_options_page');
function cancel_onhold_options_page() {
?>
<div>
<h2>Cancel On-Hold Orders after X Days</h2>
<form method="post" action="options.php">
<?php settings_fields( 'cancel_onhold_group' ); ?>
<table class="form-table">
<tr valign="top">
<th scope="row"><label for="cancel_onhold_enable">Enable/Disable this feature</label></th>
<td><input type="checkbox" id="cancel_onhold_enable" name="cancel_onhold_enable" value="1" <?php checked(get_option('cancel_onhold_enable')); ?>" /></td>
</tr>
<tr valign="top">
<th scope="row"><label for="cancel_onhold_days_delay">Enter number of days after which on hold orders will be cancelled</label></th>
<td><input type="number" id="cancel_onhold_days_delay" name="cancel_onhold_days_delay" value="<?php echo get_option('cancel_onhold_days_delay'); ?>" /> days</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}