Forked from Crocoblock/jet-booking-muliple-units.php
Created
November 23, 2022 13:15
-
-
Save castorstudio/d0eb2d1f307b5deea5b6b0c4b3182e4d to your computer and use it in GitHub Desktop.
JetBooking. Allow to book multiple units at single purchase
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Allow to book multiple units/appartments/tickets at same date/dates with single purchase | |
| * You need to add '_capacity' field into the form. This field should be responsible for number of booked items | |
| */ | |
| add_filter( 'jet-booking/form-action/pre-process', function( $result, $booking, $action ) { | |
| $capacity = absint( $action->getRequest( '_capacity' ) ); | |
| if ( ! $capacity && 1 === $capacity ) { | |
| return $result; | |
| } | |
| $booking_ids = []; | |
| $all_units = \JET_ABAF\Plugin::instance()->db->get_apartment_units( $booking['apartment_id'] ); | |
| if ( empty( $all_units ) ) { | |
| return $result; | |
| } | |
| $booked_units = \JET_ABAF\Plugin::instance()->db->get_booked_units( $booking ); | |
| $all_units_count = count( $all_units ); | |
| $booked_units_count = count( $booked_units ); | |
| if ( ( $available_units = $all_units_count - $booked_units_count ) < $capacity ) { | |
| throw new JET_ABAF\Vendor\Actions_Core\Base_Handler_Exception( | |
| 'Not enough available units. Only ' . $available_units . ' are available', | |
| 'error' | |
| ); | |
| } | |
| for ( $i = 1; $i <= $capacity; $i++ ) { | |
| $booking_id = \JET_ABAF\Plugin::instance()->db->insert_booking( $booking ); | |
| if ( $booking_id ) { | |
| $booking_ids = $booking_id; | |
| } else { | |
| throw new \JET_ABAF\Vendor\Actions_Core\Base_Handler_Exception( | |
| esc_html__( 'Booking dates already taken', 'jet-booking' ), | |
| 'error' | |
| ); | |
| } | |
| } | |
| $action->setRequest( 'booking_id', implode( ',', $booking_ids ) ); | |
| $action->setRequest( 'booking_ids', $booking_ids ); | |
| return $booking; | |
| }, 10, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment