I have built a site using WooCommerce Bookings with an AJAX request that processes the payment, creates and order and lastly creates a booking with the order ID. However, when I send the newly created order ID via the WooCommerce Bookings create method the order doesn't appear in the Booking and I have to manually connect them.
$order = wc_create_order();
$product = wc_get_product($productId);
$price = $product->get_price();
$order->add_product( $product, 1 );
$order->set_total( $price );
update_post_meta($order->id, '_customer_user', get_current_user_id() );
$order->update_status( 'completed' );
$new_booking_data = array(
'product_id' => $productId,
'start_date' => strtotime($startStr),
'end_date' => strtotime($endStr),
'user_id' => $user_ID,
'order_item_id' => $order->id, // Seems to be an issue here.
'persons' => $amountOfPeople,
'cost' => $orderTotal,
'all_day' => false,
);
$returnVal = create_wc_booking( $productId, $new_booking_data, 'pending-confirmation', true );
I know that the $order->id is valid because I'm able to use it in order to set the customer ID.
Bookings Plugin Reference: (https://docs.woothemes.com/document/creating-bookings-programatically/)
update_post_meta( $bookingId, '_booking_order_id', $order->id );
It seems a little heavy handed, but it seems to work. I'll mark this as the answer unless somebody else has a more succinct way.