[英]Automatically change order status during specific hours and days in Woocommerce
I'm looking to change the order status from processing to complete during normal business hours (8am-4pm, Monday-Friday).我希望在正常工作时间(周一至周五上午 8 点至下午 4 点)将订单状态从处理中更改为完成。
I've got the following code which works but regularly stops working我有以下代码可以正常工作但经常停止工作
date_default_timezone_set('Europe/London');
$is_week_days = in_array( date('w'), array( 1, 2, 3, 4 ) ) ? true : false; // From Monday to Thursday
$is_friday = date('w') == 5 ? true : false; // Friday
$is_week_end = in_array( date('w'), array( 0, 6 ) ) ? true : false; // Weekend days
$start_time = mktime('08', '00', '00', date('m'), date('d'), date('Y')); // 8am
$end_time = mktime('16', '00', '00', date('m'), date('d'), date('Y')); // 4pm
$now_time = time();
$after_tomorow = date('l', strtotime('+2 days'));
if (($is_week_days || $is_friday) && ($now_time >= $start_time && ($now_time < $end_time))) {
$working_hours = 1;
} else {
$working_hours = 0;
}
if ($working_hours == 1 && $status != "completed") {
$order->update_status( 'completed' );
return;
} else {
return;
}
This is part of a bigger function that is triggered once an order is placed.这是更大的 function 的一部分,一旦下订单就会触发。
Is there an easier way of doing this via functions.php?有没有更简单的方法通过functions.php来做到这一点?
Thanks!谢谢!
As Dave S commented Where does this code run now?
正如 Dave S 所评论Where does this code run now?
As I can see your code looks fine but it can improve a little better so I revised your code a little bit.正如我所看到的,您的代码看起来不错,但可以改进一些,所以我稍微修改了您的代码。
First, you added $is_week_days
and $is_friday
in the same condition so you can just add day 5
to $is_week_days
to check for Friday as well.首先,您在相同条件下添加了$is_week_days
和$is_friday
,因此您只需将第5
天添加到$is_week_days
以检查星期五。
You added extra round bracet in this condition ($now_time < $end_time)
您在这种情况下添加了额外的圆括号($now_time < $end_time)
and, you can check this $status != "completed"
case in your weekdays condition.并且,您可以在工作日的情况下检查此$status != "completed"
案例。
$is_week_days = in_array( date('w'), array( 1, 2, 3, 4, 5 ) ) ? true : false; // From Monday to Friday
$start_time = mktime( '08', '00', '00', date('m'), date('d'), date('Y') ); // 8am
$end_time = mktime( '16', '00', '00', date('m'), date('d'), date('Y') ); // 4pm
$now_time = time();
if ( $is_week_days && ( $now_time >= $start_time && $now_time <= $end_time ) && $status != "completed" ) {
$order->update_status( 'completed' );
return;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.