【问题标题】:Auto complete processing Orders after x minutes or hours in WooCommerce在 WooCommerce 中 x 分钟或小时后自动完成处理订单
【发布时间】:2021-04-07 21:26:35
【问题描述】:

基于 Automatically cancel order after X days if no payment in WooCommerce 答案代码,我做了一些更改,试图在 x 分钟 (或 x 小时)后自动完成已付款订单

    add_action( 'woocommerce_order_status_changed', 'hourly_update_status_orders', 10, 4 );
function hourly_update_status_orders( $order_id, $old_status, $new_status, $order ) {
    // Set Your shop time zone (http://php.net/manual/en/timezones.php)
    date_default_timezone_set('Europe/London');

    // Enable the process to be executed daily
    if( in_array( $new_status, array('processing') )
        && get_option( 'paid_orders_hourly_process' ) < time() ) :
    
        $time_delay       = 60 * 5; // <=== SET the time delay (here 1 hour)
        $current_time     = strtotime(str_replace(':','-', date('Y-m-d h:i:s') ));
        $targeted_time    = $current_time - $time_delay;

    // Get paid orders (10 mints old)
    $paid_orders = (array) wc_get_orders( array(
        'limit'        => -1,
        'status'       => 'processing',
        'date_created' => '<' . $targeted_time,
        'return' => 'ids',
    ) );

    if ( sizeof($paid_orders) > 0 ) {
        $completed_text = __("The order is Completed.", "woocommerce");

        // Loop through WC_Order Objects
        foreach ( $paid_orders as $paid_order ) {
            $order->update_status( 'completed', $complted_text );
        }
    }
    // Schedule the process to the next day (executed once restriction)
    update_option( 'paid_orders_hourly_process', $current_time + $time_delay );

    endif;
}

我只能让它适用于每天的变化,但不能适用于每小时的变化......

谁能帮我检查一下我哪里错了?

【问题讨论】:

  • 我删除了processing 标签。 Processing 是一个灵活的软件速写本,也是一种学习如何在视觉艺术环境中编码的语言。

标签: php wordpress woocommerce scheduled-tasks orders


【解决方案1】:

现在我正在使用此代码,它看起来比以前更有效。但仍然有间隔问题。当状态从处理变为完成时,它们之间没有间隔。

add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_change_order_status', 10, 1 );
function woocommerce_thankyou_change_order_status( $order_id ){
    if( ! $order_id ) return;

    $order = wc_get_order( $order_id );

    $time_order     = strtotime($order->get_date_created()->date('Y-m-d H:i:s'));
    $time_current   = time();
    $time_interval  = $time_current - $time_order;

    //Case refresh page after 3 hours at order, no changed status
    if( $order->get_status() == 'processing' && $time_interval < 18000 ) {
        $order->update_status( 'completed' );
    }
}

【讨论】:

    【解决方案2】:

    更新

    这些解决方案实际上并没有制定出来,而且对于每小时(或更短时间)的延迟也不是很有效,因为当下一个新订单时(或者当管理员对订单状态进行手动更改时会触发代码。

    现在您的代码中有一些错误,请尝试以下操作(未测试)

    add_action( 'woocommerce_order_status_changed', 'hourly_update_status_orders', 10, 4 );
    function hourly_update_status_orders( $order_id, $old_status, $new_status, $order ) {
        // Set Your shop time zone (http://php.net/manual/en/timezones.php)
        date_default_timezone_set('Europe/Paris');
    
        // Enable the process to be executed daily
        if( in_array( $new_status, array('processing') )
            && get_option( 'paid_orders_hourly_process' ) < time() ) :
    
        $time_delay       = 60 * 60; // <=== SET the time delay (here 1 hour)
        $current_time     = strtotime( date('Y-m-d H:00:00') );
        $targeted_time    = $current_time - $time_delay;
    
        // Get paid orders (10 mints old)
        $paid_orders = (array) wc_get_orders( array(
            'limit'        => -1,
            'status'       => 'processing',
            'date_created' => '<' . $targeted_time,
            'return' => 'ids',
        ) );
    
        if ( sizeof($paid_orders) > 0 ) {
            $completed_text = __("The order is Completed.", "woocommerce");
    
            // Loop through WC_Order Objects
            foreach ( $paid_orders as $paid_order ) {
                $order->update_status( 'completed', $complted_text );
            }
        }
        // Schedule the process to the next day (executed once restriction)
        update_option( 'paid_orders_hourly_process', $current_time + $time_delay );
    
        endif;
    }
    

    代码进入活动子主题(或活动主题)的functions.php文件中。


    如果你使用分钟而不是小时,你应该改变:

    $current_time     = strtotime( date('Y-m-d H:00:00') );
    

    到:

    $current_time     = strtotime( date('Y-m-d H:i:00') );
    

    最后说明 - 更好的选择。

    最好使用其他东西,例如由 Automatic(WordPress 和 WooCommerce 的所有者)制作的免费插件 Action scheduler,它是一个强大的调度库,已经包含在内,并且由 WooCommerce 和插件订阅使用。

    您可以在任何代码中使用它,它是比其他任何东西更好的选择。

    这里是文档用法:https://actionscheduler.org/usage/

    【讨论】:

    • 感谢您更新我的代码。我已经尝试过你的,现在订单状态不会从处理中变为完成。甚至还有代码来更新状态。
    猜你喜欢
    • 1970-01-01
    • 2021-01-07
    • 2016-04-17
    • 2019-06-01
    • 2017-04-23
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 2020-01-17
    相关资源
    最近更新 更多