1

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()
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Lubo Masura
  • 1,034
  • 6
  • 20

1 Answers1

3

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.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hello, thank you for your reply :) I very appreciate your answer. I have put this code into my child theme, but Im getting this error Fatal error: Uncaught Error: Call to a member function getId() on null. Any idea why? – Lubo Masura Mar 29 '21 at 06:59
  • This error is happening when I update date manually. For example my client calls me to change its time and then this happens. – Lubo Masura Mar 29 '21 at 07:14
  • @LuboMasura I don't use the method `getId()` on my code, so this error is due to some other code you have added. Try to remove all other related code customizations. – LoicTheAztec Mar 29 '21 at 09:32
  • That error is getting from google calendar sync, when this code is out that error does not shows up. Strange. I will try to figure it out. – Lubo Masura Mar 29 '21 at 11:50
  • Thank you anyway Loic :) – Lubo Masura Mar 29 '21 at 11:50
  • thank you for your update, it did not help still getting that error when I update date. – Lubo Masura Mar 29 '21 at 17:29
  • Where can I send you error details? Maybe we are doing it wrong. – Lubo Masura Mar 29 '21 at 17:48
  • https://jsfiddle.net/h3qrd1p7/ -this is what I get from my site. – Lubo Masura Mar 29 '21 at 17:48
  • Try to clean up (delete) older comments. You can add an edit at the end of your question, and notify me here… But I am just trying to guess things as I don't test anything... So if it doesn't work, I don't have anymore clue. – LoicTheAztec Mar 29 '21 at 17:51