我正在使用 WooCommerce Bookings 出售我的农场/宠物动物园的门票。客户可以购买特定日期和时间的门票。我通过 WooCommerce 应用程序扫描他们的订单并完成订单来签入他们。我需要一种方法,让那些从未出现过且已过日期的订单自动更新为已完成,这样他们就无法在任何一天出现并使用其票证。
我找到了 WooCommerce Bookings - 24 小时后将订单状态从“待付款”更改为“已取消” 答案代码,这正是我正在寻找的内容。但是我只希望超过日期且尚未完成的订单从“正在处理”更改为“已完成”
这是我的代码尝试:
function update_booking_status_to_completed() {
// Get all bookings
$args = array(
'post_type' => 'wc_booking',
'post_status' => 'any',
'posts_per_page' => -1,
);
$bookings = get_posts( $args );
foreach ( $bookings as $booking_post ) {
$booking = new WC_Booking( $booking_post->ID );
// Get booking start date
$start_date = $booking->get_start_date();
// Check if start date has passed
if ( strtotime( $start_date ) < current_time( 'timestamp' ) ) {
$order = wc_get_order( $booking->get_order_id() );
// Check if order status is processing
if ( $order->get_status() == 'processing' ) {
// Update order status to completed
$order->update_status( 'completed' );
}
}
}
}
如果有人可以检查的话,此代码可以工作。
最好获取所有处理订单并检查开始日期尚未过去的预订。
请尝试以下操作,这应该会自动完成处理开始日期已过的预订的订单:
// Define a custom schedule time for Cron schedules tasks
add_filter( 'cron_schedules','hourly_cron_schedule' );
function hourly_cron_schedule( $schedules ) {
if ( ! isset( $schedules["hourly"] ) ) {
$schedules["hourly"] = array(
'interval' => HOUR_IN_SECONDS,
'display' => __( 'Hourly' )
);
}
return $schedules;
}
// Define the scheduled task
add_action('init', 'my_custom_schedule_task');
function my_custom_schedule_task() {
// Schedule an action (if it's not already scheduled)
if ( ! wp_next_scheduled( 'scheduled_complete_processing_orders' ) ) {
wp_schedule_event( time(), 'hourly', 'scheduled_complete_processing_orders' );
}
}
// Function to run on the scheduled event
add_action( 'scheduled_complete_processing_orders', 'complete_processing_orders_with_bookings' );
function complete_processing_orders_with_bookings() {
// Get all processing orders
$orders = wc_get_orders( array(
'type' => 'shop_order',
'limit' => -1,
'status' => 'processing',
) );
// Lopp through processing orders
foreach ( $orders as $order ) {
// Get Bookings (IDs) from the order
$booking_ids = WC_Booking_Data_Store::get_booking_ids_from_order_id( $order->get_id() );
// Only orders with bookings
if( $booking_ids ) {
// Loop through bookings within the order
foreach ( $booking_ids as $booking_id ) {
$booking = new WC_Booking( $booking_id ); // Get the WC_Booking object
// If start date has passed
if ( strtotime( $booking->get_start_date() ) > time() ) {
$order->update_status( 'completed' );
}
}
}
}
}
代码位于子主题的functions.php 文件中(或插件中)。它可以工作。