2

I am facing a problem to add such a product to the cart.

I manage to create the booking with:

$new_booking = get_wc_booking ($new_booking_data);
$new_booking -> create ($status);

But then I tried to apply these different methods without success:

add_cart_item_data ($cart_item_meta, $product_id); // error 500

or

$ new_booking_object -> add_cart_item ($cart_item_meta); // error 500

The $cart_item_meta object I create is like the following (some keys are duplicative, like for example start and start_date, because I observed in the code that both names are used, and I wanted to be sure to send at least the correct one ;-) ). BTW, by comparing with standard bookings created via the plugin form, I can be sure that the values I set in the array have the correct format.

    $cart_item_meta = array(
        'all_day'                  => false,
        'cost'                     => $price,
        'customer_id'              => 1,
        'user_id'                  => 1,
        'date_created'             => '',
        'date_modified'            => '',
        'end'                      => $endDate,
        'end_date'                 => $endDate,
        'google_calendar_event_id' => 0,
        'order_id'                 => $order->get_id(),
        'order_item_id'            => 0,
        'parent_id'                => 0,
        'person_counts'            => array($addPaxId => $pax),
        'persons'                  => array($addPaxId => $pax),
        'product_id'               => $prodId,
        'resource_id'              => $resourceId,
        'start'                    => $startDate,
        'start_date'               => $startDate,
        'status'                   => 'in-cart',
        'local_timezone'           => 'Europe/Brussels',
    );

I do get a record in the "posts" table, with the same specifics as a normal booking created by the normal way. In particular, I get post_type = wc_booking, and post_status = in-cart. But the cart remains empty.

I have compared all the entries in the database, and I can't see what is missing. But I'm definitely missing some of the understanding of the mechanics of WooCommerce ...

Does someone can help me to find a way to pass the final step: putting the created booking in the cart ? Thanks!

1 Answers1

1

Well, maybe someone will be interested to know how I worked around, as so far I couldn't find a complete programming solution.

The products I want to book programmatically and add to the cart are existing products in my WooCommerce store. My purpose is just to use my own form to allow the customer to better understand what he books (I'm managing a flight simulators center with several machines), and the native WC Bookings form is not clear enough for me, and moreover it is really heavy and slow.

So after gathering customers info with my own form (simulator, duration, participants, timeslot), my problem was to put all this in the cart. I could achieve to create the relevant booking in the database, but not add it to the cart.

I observed that when you book via the standard form, you send a POST to the server on the same product page, including the following parameters (random data for example):

wc_bookings_field_persons_xxxx     => 2      // 'xxxx' is the ID of the related 'bookable_person' in 'posts'
wc_bookings_field_start_date_month => 11     // November (sample)   
wc_bookings_field_start_date_day   => 26     // the 26th (sample)
wc_bookings_field_start_date_year  => 2021   // Year (sample)
wc_bookings_field_start_date_time  => 2021-11-26T15:00:00+0100
wc_bookings_field_start_date_local_timezone => Europe/Brussels
add-to-cart                        => 1147   // the product ID in 'posts'

So the simplest solution is to create a POST call (via form + button or button + ajax), setting the action/ajax url to the related product after filling the fields here above with the custom form...

EDIT: while I was thinking this was the solution, I discovered that it worked only when another tab in my browser previously 'opened the door' by adding the same product in the standard way. But when this tab is closed (or timeout), the above method fails and just open the product page. The 302 redirect that was operating stops to work for un unknown reason, and we get the standard 200 straight forward. No Cart.

So the question is still pending.