Search code examples
phpwordpresswoocommerceorderswoocommerce-bookings

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');
            }
        }
    }
}

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?


Solution

  • Note that get_bookings_for_order() method doesn't exist in WooCommerce Bookings. That could be the issue that is throwing an error.

    Now as orders paid with Cash On Delivery (cod) payment method, always get a processing status, is better to use woocommerce_order_status_processing hook like:

    add_action('woocommerce_order_status_processing', 'set_booking_status_confirmed_for_cod_payment', 10,  2);
    function set_booking_status_confirmed_for_cod_payment( $order_id, $order  ) {
        // Check if the payment method is Cash on Delivery
        if ( $order->get_payment_method() === 'cod' ) {
            // Get all booking Ids from the order ID
            $booking_ids = (array) WC_Booking_Data_Store::get_booking_ids_from_order_id( $order_id );
    
            if ( $booking_ids ) {
                // Loop through bookings
                foreach ( $booking_ids as $booking_id ) {
                    // Get an instance of the WC_Booking Object
                    $booking = new WC_Booking( $booking_id );
                    
                    // Update booking status to confirmed if it's unpaid or pending
                    if ( in_array( $booking->get_status(), ['unpaid', 'pending'], true ) ) {
                        $booking->update_status('confirmed');
                    }
                }
            }
        }
    }
    

    It should work.