/** * When a booking is added to the cart, validate it * * @param mixed $passed * @param mixed $product_id * @param mixed $qty * @return bool */ public function validate_add_cart_item($passed, $product_id, $qty) { $product = get_product($product_id); if ($product->product_type !== 'booking') { return $passed; } $booking_form = new WC_Booking_Form($product); $data = $booking_form->get_posted_data(); $validate = $booking_form->is_bookable($data); if (is_wp_error($validate)) { wc_add_notice($validate->get_error_message(), 'error'); return false; } return $passed; }
/** * Get all available booking products * * @param string $start_date YYYY-MM-DD format * @param string $end_date YYYY-MM-DD format * @param int $quantity Number of people to book * @return array Available post IDs */ function get_available_bookings($start_date, $end_date, $quantity = 1) { $matches = array(); $start_date = explode(' ', $start_date); $end_date = explode(' ', $end_date); $start = explode('-', $start_date[0]); $end = explode('-', $end_date[0]); $args = array('wc_bookings_field_persons' => $quantity, 'wc_bookings_field_duration' => 1, 'wc_bookings_field_start_date_year' => $start[0], 'wc_bookings_field_start_date_month' => $start[1], 'wc_bookings_field_start_date_day' => $start[2], 'wc_bookings_field_start_date_to_year' => $end[0], 'wc_bookings_field_start_date_to_month' => $end[1], 'wc_bookings_field_start_date_to_day' => $end[2]); // Loop through all posts foreach ($this->product_ids as $post_id) { if ('product' == get_post_type($post_id)) { $product = wc_get_product($post_id); if (is_wc_booking_product($product)) { // Support time if ('hour' == $product->get_duration_unit()) { if (!empty($start_date[1])) { $args['wc_bookings_field_start_date_time'] = $start_date[1]; } } // Support WooCommerce Accomodation Bookings plugin // @src woocommerce-bookings/includes/booking-form/class-wc-booking-form.php $unit = 'accommodation-booking' == $product->product_type ? 'night' : 'day'; $duration = $this->calculate_duration($start_date[0], $end_date[0], $unit); $args['wc_bookings_field_duration'] = $duration; $booking_form = new WC_Booking_Form($product); $posted_data = $booking_form->get_posted_data($args); // Returns WP_Error on fail if (true === $booking_form->is_bookable($posted_data)) { $matches[] = $post_id; } } } } return $matches; }