【问题标题】:WooCommerce Bookings - Change Order status from "Pending Payment" to "Cancelled" after 24 hoursWooCommerce 预订 - 24 小时后将订单状态从“待付款”更改为“已取消”
【发布时间】:2020-06-24 18:50:19
【问题描述】:

我正在使用 WooCommerce 预订,客户可以在其中请求需要管理员确认的预订。如果管理员未确认预订,我希望在 24 小时后自动取消订单。

来自 Change WooCommerce order status after X time has passed 答案代码,我正在寻找任何帮助来满足我的要求,使用 WP cron。

【问题讨论】:

  • 当您使用来自 StackOverFlow 的现有答案代码时,请不要在您的问题中添加此答案代码,只需该答案代码的链接即可。
  • 请原谅@LoicTheAztec - 在我的最后一次编辑中,我绝对想参考原始帖子,但也方便任何人在不离开页面的情况下查看代码。明白了。对不起

标签: php wordpress woocommerce woocommerce-bookings


【解决方案1】:

我将带着一个可行的解决方案来满足我的要求。我希望它可以帮助某人。

计划每 30 分钟运行一次事件,检查超过 24 小时的待处理订单并取消它们。

 * 创建自定义的预定钩子
 *
 * @param $schedules
 * @return 混合
 */
功能 custom_cron_schedule( $schedules ) {
    if ( !isset( $schedules["30min"] ) ) {
        $schedules["30min"] = array(
            '间隔' => 30 * 60,
            'display' => __( '每 30 分钟一次' )
        );
    }
    返回 $schedules;
}
add_filter('cron_schedules','custom_cron_schedule');

//如果还没有调度,则调度一个动作
如果(!wp_next_scheduled('custom_pending_orders_hook')){
    wp_schedule_event(时间(),'30分钟','custom_pending_orders_hook');
}

add_action('custom_pending_orders_hook', 'custom_cancel_pending_orders');

/**
 * 在预定事件上运行的功能,检查超过 24 小时的订单
 *
 */
功能 custom_cancel_pending_orders() {
    // 获取超过 24 小时且处于待处理状态的预订
    $args = 数组(
        'post_type' => 'wc_booking',
        'posts_per_page' => -1,
        'post_status' => '待确认',
        'date_query' => 数组(
            大批(
                '之前' => '24 小时前' // 小时前
            )
        )
    );

    // 做查询
    $查询 = 新 \WP_Query( $args );

    // 循环
    if ( $query->have_posts() ) {
        // 获取帖子
        $bookings = $查询->帖子;

        // 遍历帖子
        foreach ( $bookings 作为 $booking ) {
            if ( class_exists('WC_Booking') ) {
                // 使用帖子的 id 获取预订对象
                $wc_booking = 新 \WC_Booking( $booking->ID );

                // 改变状态
                如果($wc_booking){
                    $wc_booking->update_status('取消');
                }
            }
        }
    } 别的 {
        // 没有找到帖子,继续
    }

    // 恢复原始 Post 数据
    wp_reset_postdata();
}

【讨论】:

  • 谢谢,您的解决方案帮助我作为我的函数的起始代码 :)
猜你喜欢
  • 2019-08-12
  • 2020-11-03
  • 2019-05-18
  • 2018-01-10
  • 2016-08-26
  • 2017-05-24
  • 2019-02-21
  • 1970-01-01
  • 2019-05-18
相关资源
最近更新 更多