Update Booking status to confirmed for cod orders in WooCommerce

Описание к видео Update Booking status to confirmed for cod orders in WooCommerce

Hello everyone! I hope this video has helped solve your questions and issues. This video is shared because a solution has been found for the question/problem. I create videos for questions that have solutions. If you have any other issues, feel free to reach out to me on Instagram:   / ky.emrah  

Below, you can find the text related to the question/problem. In the video, the question will be presented first, followed by the answers. If the video moves too fast, feel free to pause and review the answers. If you need more detailed information, you can find the necessary sources and links at the bottom of this description. I hope this video has been helpful, and even if it doesn't directly solve your problem, it will guide you to the source of the solution. I'd appreciate it if you like the video and subscribe to my channel!Update Booking status to confirmed for cod orders in WooCommerce

I am using WooCommerce and WooCommerce Bookings plugins.
I would like to update the status of all bookings paid with "Cash on Delivery" payment method to "Confirmed", when they have "unpaid" or "pending" status. Currently, they are being set to "unpaid".
Here is my code attempt:
add_action('woocommerce_thankyou', 'set_cod_booking_status_to_confirmed', 10, 1);
function set_cod_booking_status_to_confirmed($order_id) {
$order = wc_get_order($order_id);

// Check if the payment method is Cash on Delivery
if ( $order- get_payment_method() === 'cod' ) {
// Get all bookings associated with this order
$bookings = WC_Bookings_Controller::get_bookings_for_order($order_id);

// Check if there are bookings
if (!empty($bookings)) {
// Get the first booking (if you want to confirm only the first one)
$booking = reset($bookings);

// Update the status to confirmed if it's unpaid or pending
if ($booking- get_status() === 'unpaid' || $booking- get_status() ===
'pending') {
$booking- update_status('confirmed');
}
}
}
}

add_action('woocommerce_thankyou', 'set_cod_booking_status_to_confirmed', 10, 1);
function set_cod_booking_status_to_confirmed($order_id) {
$order = wc_get_order($order_id);

// Check if the payment method is Cash on Delivery
if ( $order- get_payment_method() === 'cod' ) {
// Get all bookings associated with this order
$bookings = WC_Bookings_Controller::get_bookings_for_order($order_id);

// Check if there are bookings
if (!empty($bookings)) {
// Get the first booking (if you want to confirm only the first one)
$booking = reset($bookings);

// Update the status to confirmed if it's unpaid or pending
if ($booking- get_status() === 'unpaid' || $booking- get_status() ===
'pending') {
$booking- update_status('confirmed');
}
}
}
}

Unfortunately, it don't work as expected. When I create a new booking order, on the booking page it throws an error saying "An error was encountered while creating your booking". However, the booking is still successfully created with the status "unpaid".
How can I update the booking status to confirmed for cod orders in WooCommerce?


Tags: php,wordpress,woocommerce,orders,woocommerce-bookingsSource of the question:
https://stackoverflow.com/questions/7...

Question and source license information:
https://meta.stackexchange.com/help/l...
https://stackoverflow.com/

Комментарии

Информация по комментариям в разработке