# Cancel user booking from code

**URL:** <https://community.hivepress.io/t/cancel-user-booking-from-code/3484>\
**Category:** Bookings\
**Tags:** code\
**Created:** [December 20, 2022, 9:29am UTC](https://community.hivepress.io/t/cancel-user-booking-from-code/3484 "2022-12-20T09:29:59Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![saagark22](https://community.hivepress.io/user_avatar/community.hivepress.io/saagark22/32/3256_2.png) [@saagark22](https://community.hivepress.io/u/saagark22)\
**Post date:** [December 20, 2022, 9:29am UTC](https://community.hivepress.io/t/cancel-user-booking-from-code/3484/1 "2022-12-20T09:29:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ihor](https://community.hivepress.io/user_avatar/community.hivepress.io/ihor/32/5_2.png) [@ihor](https://community.hivepress.io/u/ihor)\
**Post date:** [December 20, 2022, 9:38am UTC](https://community.hivepress.io/t/cancel-user-booking-from-code/3484/2 "2022-12-20T09:38:29Z")

</div>

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();`

---

<div class="post-metadata">

**Author:** ![saagark22](https://community.hivepress.io/user_avatar/community.hivepress.io/saagark22/32/3256_2.png) [@saagark22](https://community.hivepress.io/u/saagark22)\
**Post date:** [December 20, 2022, 10:10am UTC](https://community.hivepress.io/t/cancel-user-booking-from-code/3484/3 "2022-12-20T10:10:13Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![ihor](https://community.hivepress.io/user_avatar/community.hivepress.io/ihor/32/5_2.png) [@ihor](https://community.hivepress.io/u/ihor)\
**Post date:** [December 21, 2022, 9:05am UTC](https://community.hivepress.io/t/cancel-user-booking-from-code/3484/4 "2022-12-21T09:05:16Z")

</div>

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](https://docs.hivepress.io/developer-docs/tutorials/create-a-custom-hivepress-extension)

---

<div class="post-metadata">

**Author:** ![saagark22](https://community.hivepress.io/user_avatar/community.hivepress.io/saagark22/32/3256_2.png) [@saagark22](https://community.hivepress.io/u/saagark22)\
**Post date:** [December 21, 2022, 9:21am UTC](https://community.hivepress.io/t/cancel-user-booking-from-code/3484/5 "2022-12-21T09:21:37Z")

</div>

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”

---

<div class="post-metadata">

**Author:** ![ihor](https://community.hivepress.io/user_avatar/community.hivepress.io/ihor/32/5_2.png) [@ihor](https://community.hivepress.io/u/ihor)\
**Post date:** [December 21, 2022, 5:41pm UTC](https://community.hivepress.io/t/cancel-user-booking-from-code/3484/6 "2022-12-21T17:41:59Z")

</div>

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);`

---

<div class="post-metadata">

**Author:** ![saagark22](https://community.hivepress.io/user_avatar/community.hivepress.io/saagark22/32/3256_2.png) [@saagark22](https://community.hivepress.io/u/saagark22)\
**Post date:** [December 21, 2022, 5:46pm UTC](https://community.hivepress.io/t/cancel-user-booking-from-code/3484/7 "2022-12-21T17:46:34Z")

</div>

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

Could you please help further?

---

<div class="post-metadata">

**Author:** ![ihor](https://community.hivepress.io/user_avatar/community.hivepress.io/ihor/32/5_2.png) [@ihor](https://community.hivepress.io/u/ihor)\
**Post date:** [December 22, 2022, 6:55am UTC](https://community.hivepress.io/t/cancel-user-booking-from-code/3484/8 "2022-12-22T06:55:37Z")

</div>

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

```auto
add_actuion('init', 'my_custom_function');

function my_custom_function() {
// custom code here
}

```

---

<div class="post-metadata">

**Author:** ![saagark22](https://community.hivepress.io/user_avatar/community.hivepress.io/saagark22/32/3256_2.png) [@saagark22](https://community.hivepress.io/u/saagark22)\
**Post date:** [December 22, 2022, 12:40pm UTC](https://community.hivepress.io/t/cancel-user-booking-from-code/3484/9 "2022-12-22T12:40:34Z")

</div>

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()

---

<div class="post-metadata">

**Author:** ![ihor](https://community.hivepress.io/user_avatar/community.hivepress.io/ihor/32/5_2.png) [@ihor](https://community.hivepress.io/u/ihor)\
**Post date:** [December 23, 2022, 7:47am UTC](https://community.hivepress.io/t/cancel-user-booking-from-code/3484/10 "2022-12-23T07:47:37Z")

</div>

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

```auto
$listing=$booking->get_listing();

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

```

---

<div class="post-metadata">

**Author:** ![ihor](https://community.hivepress.io/user_avatar/community.hivepress.io/ihor/32/5_2.png) [@ihor](https://community.hivepress.io/u/ihor)\
**Post date:** [December 23, 2022, 7:49am UTC](https://community.hivepress.io/t/cancel-user-booking-from-code/3484/11 "2022-12-23T07:49:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://community.hivepress.io/user_avatar/community.hivepress.io/system/32/13990_2.png) [@system](https://community.hivepress.io/u/system)\
**Post date:** [January 22, 2023, 7:49am UTC](https://community.hivepress.io/t/cancel-user-booking-from-code/3484/12 "2023-01-22T07:49:38Z")

</div>

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