0

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/)

1
  • I tried doing: 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.
    – adam2k
    Apr 5, 2016 at 21:14

2 Answers 2

1

A minor update (I ran into this issue today):

The above solution does not work anymore. The only thing you need to do nowadays is to set the post_parent:

wp_update_post(array(

    'ID' => $booking->id,
    'post_parent' => $order->id

));
0

Building on the answer by Gerben Van Dijk, we need to update the parent of the order item, rather than the order itself.

If there are multiple items in the order - you may require more conditional logic than this, but this will attach the booking to the first item. If there's only one cart item - no problemo! Not an elegant solution - but the built in booking functions work when referring to a booking on my site. For instance, "$booking->get_order_item_id()" did not work later on unless I attached it like this:

foreach ( $order->get_items() as $item ) {
  wp_update_post( array(
            'ID'          => $returnVal->id, // Booking object after success
            'post_parent' => $item->get_id()
        ) );
  break;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.