3

I have been trying this for a couple of hours now, with different code & reading every doc on booking I can find - but I seem to be unable to figure out how exactly I do this.

My biggest inspiration comes from the WooCommerce Booking Doc, but this just adds a follow-up booking to an existing order. But how do I go about doing it from scratch?

I tried the following, but cant really get it to work.

  1. I generate a new order with:

    $address = array(
        'first_name' => 'TestFirst',
        'last_name'  => 'TestLast',
        'company'    => 'Overflow',
        'email'      => 'test@test.com',
        'phone'      => '777-777-777-777',
        'address_1'  => '35 Main Street',
        'address_2'  => '', 
        'city'       => 'Net York',
        'state'      => 'NY',
        'postcode'   => '2323',
        'country'    => 'US'
    
    $order = wc_create_order();
    $order->set_address( $address, 'billing' );
    $order->set_address( $address, 'shipping' );
    
  2. I create a booking by

    $new_booking_data = array(
            'start_date'  => strtotime( '+1 week', $prev_booking->start ), // same time, 1 week on
            'end_date'    => strtotime( '+1 week', $prev_booking->end ), // same time, 1 week on
            'resource_id' => $prev_booking->resource_id, // same resource
            'parent_id'   => $booking_id
    )
    create_wc_booking( $product_id, $new_booking_data = array(), $status =     'confirmed', $exact = false )
    
  3. And here I'm stuck, what do I have to do now? I figure I have to connect the booking ID with the order from 1. - but I'm not really sure how.

What I am doing wrong?

Thanks.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Mac
  • 334
  • 1
  • 3
  • 13

2 Answers2

3

In your code you are defining this array first (with a missing ; at the end of the array).

Then instead of using $new_booking_data defined variable simply in your function create_wc_booking(), you are assigning to it an empty array, that NULLs the code above. So you have to set it just like this:

// Defined array variable
$new_booking_data = array(
        'start_date'  => strtotime( '+1 week', $prev_booking->start ), // same time, 1 week on
        'end_date'    => strtotime( '+1 week', $prev_booking->end ), // same time, 1 week on
        'resource_id' => $prev_booking->resource_id, // same resource
        'parent_id'   => $booking_id
); // <= Missing ";" HERE

// Define this variables outside your function
$status = 'confirmed';
$exact = false;

// Now you just put your variables simply like this
create_wc_booking( $product_id, $new_booking_data, $status, $exact );

Or you can put the values too, this way:

create_wc_booking( $product_id, $new_booking_data, 'confirmed', false );

This should better work now… I hope.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hallo thank you for your comment, i think you misunderstood me a little - my problem was how to connect a WooCommerce order and a Woocommerce booking. And as a additional question, if you also add in the WooCommerce Product Add-ons, how do i add that aswell to the Booking Product? – Mac Feb 17 '17 at 22:34
0

I had the same problem, i was trying adding order_item_id to $new_booking_data but it not work.

The solution was here: https://wordpress.stackexchange.com/questions/222799/woocommerce-bookings-plugin-not-saving-order-id

Gerben Van Dijk is the autor of this solution.