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

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

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

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

   
  /**
  * Code goes in theme functions.php
  *
  * In this example we’re creating a booking 1 week after a booking is paid for.
  * This does not create another order or payment, just an additional booking.
  * $exact is false meaning if our slot is taken, the next available slot will be used.
  * @link https://docs.woocommerce.com/document/creating-bookings-programatically/
  */
  function auto_create_followup_booking( $booking_id ) {
   
  // Get the previous booking from the ID
  $prev_booking = get_wc_booking( $booking_id );
   
  // Don’t want follow ups for follow ups
  if ( $prev_booking->get_parent_id() <= 0 ) {
  // Set the follow up data
  $new_booking_data = array(
  ‘start_date’ => strtotime( ‘+1 week’, $prev_booking->get_start() ), // same time, 1 week on
  ‘end_date’ => strtotime( ‘+1 week’, $prev_booking->get_end() ), // same time, 1 week on
  ‘resource_id’ => $prev_booking->get_resource_id(), // same resource
  ‘parent_id’ => $booking_id, // set the parent
  );
  // Did the previous booking have persons?
  $persons = $prev_booking->get_persons();
  if ( is_array( $persons ) && 0 < count( $persons ) ) {
  $new_booking_data[‘persons’] = $persons;
  }
  // Was the previous booking all day?
  if ( $prev_booking->is_all_day() ) {
  $new_booking_data[‘all_day’] = true;
  }
  create_wc_booking(
  $prev_booking->get_product_id(), // Creating a booking for the previous bookings product
  $new_booking_data, // Use the data pulled above
  $prev_booking->get_status(), // Match previous bookings status
  false // Not exact, look for a slot
  );
  }
  }
  add_action( ‘woocommerce_booking_in-cart_to_paid’, ‘auto_create_followup_booking’ );
  add_action( ‘woocommerce_booking_unpaid_to_paid’, ‘auto_create_followup_booking’ );
  add_action( ‘woocommerce_booking_confirmed_to_paid’, ‘auto_create_followup_booking’ );
Was this article helpful?
Dislike 0
Views: 16