2

I have a form on a website that allows users to create bookable products. However, I can't find how to create a availability interval using Woocommerce php functions. Does anyone have an idea?

Here's how I create my product

$post_id = wp_insert_post( array(
    'post_title' => $_POST["title"],
    'post_content' => $_POST["description"],
    'post_status' => 'publish',
    'post_type' => "product",
) );
wp_set_object_terms( $post_id, 'booking', 'product_type' );

In another part of this same project, I use the get_post_meta function to get the availabilities. I get that the availabilities are meta data from the post but I'm not exacly sure how to change these. I tried using the usual add_post_meta( $post_id, '_wc_booking_availability', $availability ); but that didn't work. Is there something I'm missing about Woocommerce availabilities?

On a side note, is there a more detailed documentation of Woocommerce Booking?

The developer documentation on their website only covers the basics of creating a product programmatically.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Pierre-Alexis
  • 153
  • 1
  • 8

1 Answers1

4

I found an answer if anyone else needs it.

//This is the array that will contain all the availabilities
$availabilities = array();

//Create an array that contains the required fields (from_date and to_date are not necessary in some types of availabilities) 
$availability = array(
        'type'      => 'time:range', //Replace time:range with the type you need
        'bookable'  => 'yes',
        'priority'  => 10,
        'from'      => wc_booking_sanitize_time( "01:00" ),
        'to'        => wc_booking_sanitize_time( "03:00" ),
        'from_date' => wc_clean( "2018-10-02" ),
        'to_date'   => wc_clean( "2018-10-02" )
);

//If you need to add more than one availability, make a loop and push them all into an array
array_push($availabilities, $availability);
add_post_meta( $post_id, '_wc_booking_availability', $availabilities );

I found it here but I modified it for my needs https://wordpress.stackexchange.com/questions/233904/how-to-add-date-range-in-woocommerce-with-code

Pierre-Alexis
  • 153
  • 1
  • 8