Cancel user booking from code

Is there any hook/action/function we can use to cancel user booking from the code?

I have grabbed the booking ID in a variable in the code. So something like hpCancelBooking(Booking ID); will also help.

Remember, this cancellation should also trigger relevant cancellation email notifications.

If you already for the booking object:

$booking=\HivePress\Models\Booking::query()->get_by_id(123);

You can cancel booking by changing its status to trash:

$booking->trash();

Or this way:

$booking->set_status('trash')->save_status();

Thanks Ihor,

I’m actually building a custom plugin to extend the functionality of Hivepress for my website.

Now question is, will $booking=\HivePress\Models\Bookings::query()->get_by_id(123); be available on my plugin. Or first I need to include some files from Hivepress in my plugin?

Yes, it will be available if HivePress plugin is also active, you can add a function_exists('hivepress') check before any HivePress-specific code to prevent errors if HivePress accidentally gets deactivated.

If you’ve created this custom plugin to customize HivePress, the best way is to structure it as a HivePress extension, then all the related files will be loaded automatically https://docs.hivepress.io/developer-docs/tutorials/create-a-custom-hivepress-extension

Thanks Ihor for getting back.

Both plugins Hivepress and Hivepress booking are activated but still I get error

Fatal error : Uncaught Error: Class “HivePress\Models\Bookings”

Sorry, it seems that I used “Bookings” in the snippet instead of “Booking”, this one should be ok:

$booking=\HivePress\Models\Booking::query()->get_by_id(123);

Yes, I tried with booking but now it works from functions.php but not from custom plugin.

Could you please help further?

Make sure that your plugin is loaded later than HivePress, you can do this by running your code on init action:

add_actuion('init', 'my_custom_function');

function my_custom_function() {
// custom code here
}

When I fetch anything from booking object such as:
$listingTitle = $booking->get_listing()->get_title();

I get error
Fatal error : Uncaught Error: Call to undefined method WP_Error::get_id()

Maybe this booking has no linked listing? It’s better to check if the object exists first:

$listing=$booking->get_listing();

if($listing) {
// ...
}

By the way, are you a developer or you’re building a website for yourself? If you’re a developer, you can join our HivePress Experts program and we’ll recommend your freelancer profile (e.g. via Fiverr, Upwork, Codeable) for customizations. The program will be launched this weekend.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.