When the customers do not finish their payments after selecting a payment gateway on the WooCommerce Checkout page, the status of the Booking is set to Pending Admin. It was done to let the guests finish the transaction. Thus, it may require more attention from the website administrator or owner.
While some owners prefer to cancel such Bookings automatically, there is a workaround to get the Booking canceled immediately if it is not paid during a specific timeframe. You can follow the steps below to set it up:
– go to WooCommerce > Settings > Products > Inventory
– set Hold stock (minutes) as much as you need, e.g., 15 minutes. So if the payment is not finished within 15 minutes, the status of the WooCommerce Order will be changed to Canceled. However, this status will not change the status of the Booking. So here is a code below that switches the status of the unpaid Order to Failed, and this should change the status of the Booking to Canceled automatically.
remove_action( 'woocommerce_cancel_unpaid_orders', 'wc_cancel_unpaid_orders' );
add_action( 'woocommerce_cancel_unpaid_orders', 'theme_wc_cancel_unpaid_orders' );
function theme_wc_cancel_unpaid_orders() {
$held_duration = get_option( 'woocommerce_hold_stock_minutes' );
if ( $held_duration < 1 || 'yes' !== get_option( 'woocommerce_manage_stock' ) ) {
return;
}
$data_store = WC_Data_Store::load( 'order' );
$unpaid_orders = $data_store->get_unpaid_orders( strtotime( '-' . absint( $held_duration ) . ' MINUTES', current_time( 'timestamp' ) ) );
if ( $unpaid_orders ) {
foreach ( $unpaid_orders as $unpaid_order ) {
$order = wc_get_order( $unpaid_order );
if ( apply_filters( 'woocommerce_cancel_unpaid_order', 'checkout' === $order->get_created_via(), $order ) ) {
$order->update_status( 'failed', __( 'Unpaid order cancelled - time limit reached.', 'woocommerce' ) );
}
}
}
wp_clear_scheduled_hook( 'woocommerce_cancel_unpaid_orders' );
wp_schedule_single_event( time() + ( absint( $held_duration ) * 60 ), 'woocommerce_cancel_unpaid_orders' );
}
You may add the code to the functions.php file of your child theme or by using a third-party plugin like Code Snippets.
Comments
0 comments
Article is closed for comments.