1. Documentation /
  2. Introduction to WooCommerce Bookings /
  3. Creating Bookings Programatically

Creating Bookings Programatically

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 with code/templates and resolving potential conflicts, select a WooExpert or Developer for assistance. We are unable to provide support for customizations under our  Support Policy.

The create_wc_booking function

↑ Back to top

The function takes the following arguments:

create_wc_booking( $product_id, $new_booking_data = array(), $status = 'confirmed', $exact = false )

  1. Product ID: The id of the bookable product which you are creating a new booking for.
  2. New Booking data: Array of booking data. See “booking data array” below.
  3. Status: Status of the new booking. Valid statuses include: ‘unpaid’, ‘pending’, ‘confirmed’, ‘cancelled’, ‘complete’
  4. 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 top

The $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 for
  • order_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 counts
  • cost – cost of the booking
  • all_day – true or false, if this is an all day booking
  • parent_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 top

This example shows how to create a follow-up booking, one week after a new booking is made.

View on Github