phpwordpresswoocommercegoogle-calendar-apiwoocommerce-bookings

Exclude a product from WooCommerce Booking Google calendar sync in unpaid status


I have an issue with excluding 1 product from woocommerce bookings Google calendar sync in unpaid status. I have a clients which are booking individual classes where they pay at. These classes I need to sync with my google calendar in unpaid status. 1 product is for more people and each of them have to pay before the lesson starts. I would like to sync these bookings once they are paid into my calendar.

I use this code to get unpaid bookings to my google calendar.

add_filter('woocommerce_booking_is_paid_statuses', 'woocommerce_booking_add_unpaid_to_is_paid_statuses');
function woocommerce_booking_add_unpaid_to_is_paid_statuses( $statuses ) { 
$statuses[]= 'unpaid';
return $statuses;
}

I need to exclude product id 946 from this function. Could you please give me some advice how to do it? Thank you!

UPDATE

I've tried to modify function like this, but I'm still getting some error in wp.

Code

function woocommerce_bookings_kalendar( $statuses, $order ) { 
// Get items in order
    $items = $order->get_items(); 

// Loop for all items
    foreach ( $items as $item ) {
       $product_id = $item['product_id'];

if ( $product_id == 946 ) {
            $statuses[]= 'unpaid';

return $statuses;
}
}
}
add_filter('woocommerce_booking_is_paid_statuses', 'woocommerce_bookings_kalendar');

Error:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function woocommerce_bookings_kalendar()

Solution

  • Update 2

    To avoid "Fatal error: Uncaught ArgumentCountError: Too few arguments: …" error, should need to declare the number of hook arguments (variables), when there are more than one, in the add_filter() function like:

    add_filter( 'woocommerce_booking_is_paid_statuses', 'filter_booking_is_paid_statuses', 10, 2 );
    function filter_booking_is_paid_statuses( $statuses, $order ) { 
        // Do something
    
        return $statuses;
    }
    

    This will solve this error.

    But the hook woocommerce_booking_is_paid_statuses, located in includes/class-wc-bookings-google-calendar-connection.php file of WooCommerce booking plugin and has a unique variable argument $statuses.

    So in your code $order will be always null and $items = $order->get_items(); will throw another error.

    So it's not possible to get order details using this hook.


    To exclude specific unpaid booking from WooCommerce Booking Google calendar sync, you could try to use additionally woocommerce_bookings_gcalendar_sync filter hook.

    So your code replacement will be:

    add_filter( 'woocommerce_booking_is_paid_statuses', 'filter_booking_is_paid_statuses' );
    function filter_booking_is_paid_statuses( $statuses ) { 
        return array_merge( $statuses, array('unpaid') );
    }
    
    add_filter( 'woocommerce_bookings_gcalendar_sync', 'filter_bookings_gcalendar_sync', 10, 2 );
    function filter_booking_has_status( $event, $booking ){
        $excl_product_id = 946;
    
        if( $booking->get_status() === 'unpaid' && $booking->get_product_id() !== $excl_product_id ) {
            $event = new Google_Service_Calendar_Event(); // Get an empty event Object
            $event->setid( $booking->get_google_calendar_event_id() ); // Set Id
        }
        return $event;
    }
    

    Code goes in functions.php file of the active child theme (or active theme). Untested it could work.