Bookings can be created programmatically using PHP, should you wish to create a follow-up booking or bookings from other plugins. The function you use is create_wc_booking.
Note:
This is a Developer level doc. If you are unfamiliar working with code and resolving potential conflicts, we recommend you work with a Woo Agency Partner for larger projects, or find a WooCommerce developer on Codeable for smaller customizations. We are unable to provide support for customizations under our Support Policy.
The create_wc_booking function
↑ Back to topThecreate_wc_booking
function takes the following arguments:
create_wc_booking( $product_id, $new_booking_data = array(), $status = 'confirmed', $exact = false )
- Product ID: The id of the bookable product which you are creating a new booking for.
- New Booking data: Array of booking data. See “booking data array” below.
- Status: Status of the new booking. Valid statuses include: ‘unpaid’, ‘pending’, ‘confirmed’, ‘cancelled’, ‘complete’
- Exact: true or false – If false, the function will look for the next available slot after your start date, if the date you tried to book is unavailable.
Booking Data Array
↑ Back to topThe $new_booking_data argument is passed to your new booking. By default, it consists of the following:
$defaults = array(
'product_id' => $product_id, // Booking ID
'start_date' => '',
'end_date' => '',
'resource_id' => '',
);
You’ll likely want to pass the start and end date of your new booking, in which case you should pass in a Unix timestamp.
Resource ID is optional and should only be passed if your bookable product has multiple resources. You can find the ID by looking at the resources section when editing the product.
The following non-default arguments are also supported for your booking data:
user_id
– ID of the user this booking is fororder_item_id
– If linking the booking to an order with a bookable product inside, the id of the order item.persons
– count of persons this booking is for or an array of person types with countscost
– cost of the bookingall_day
– true or false, if this is an all day bookingparent_id
– If this is a follow up to an existing booking, this is the previous booking’s ID
Use case: Creating a one-week follow-up
↑ Back to topThis example shows how to create a follow-up booking, one week after a new booking is made.