简体   繁体   English

在 Woocommerce 中的特定时间和日期自动更改订单状态

[英]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.

相关问题 PHP代码自动更改WooCommerce订单状态 - PHP code for automatically change WooCommerce order status 根据已批准状态和特定订单项目更改 WooCommerce 订单状态 - Change WooCommerce order status based on approved status and specific order item 批量更改订单信息作为特定Woocommerce订单ID的订单状态 - Batch change order information as order status for specific Woocommerce order IDs 自动更改 WooCommerce 中“预购”的订单状态 - Automatically change order status for "pre-orders" in WooCommerce 在 WooCommerce 特定订单状态更改上以编程方式添加免费产品 - Add a free product programmatically on WooCommerce specific order status change WooCommerce 中特定产品的自动更改订单状态为已完成 - Auto change order status to completed for specific products in WooCommerce 防止 WooCommerce 订单状态从/到特定状态更改 - Prevent WooCommerce order status Change from / to specific statuses WooCommerce 预订 - 24 小时后将订单状态从“待付款”更改为“已取消” - WooCommerce Bookings - Change Order status from “Pending Payment” to “Cancelled” after 24 hours 如何将 woocommerce 加工状态自动更改为已完成 - How to automatically change woocommerce processing status to completed WooCommerce中特定订单状态如何减少库存 - How to reduce the stock for specific order status in WooCommerce
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM