diff --git a/src/Livewire/AvailabilityResults.php b/src/Livewire/AvailabilityResults.php index 0a9fb97..20bf719 100644 --- a/src/Livewire/AvailabilityResults.php +++ b/src/Livewire/AvailabilityResults.php @@ -158,6 +158,9 @@ public function getAvailability(): void public function checkout(): void { + if ($this->extraDays !== 0 && $this->availability->count() > 1) { + $this->availability = collect($this->availability->get(0)); + } try { $this->validateAvailabilityAndPrice(); $this->createReservation(); diff --git a/tests/Livewire/AvailabilityResultsTest.php b/tests/Livewire/AvailabilityResultsTest.php index 1da8eb9..61efc2a 100644 --- a/tests/Livewire/AvailabilityResultsTest.php +++ b/tests/Livewire/AvailabilityResultsTest.php @@ -795,6 +795,52 @@ public function test_creates_reservation_and_saves_custom_data_in_the_database() ); } + // Test that it creates a reservation when multiple days results are enabled + public function test_checkout_works_for_multiple_days() + { + $entry = Entry::make() + ->collection('pages') + ->slug('checkout') + ->data(['title' => 'Checkout']); + + $entry->save(); + + Config::set('resrv-config.checkout_entry', $entry->id()); + + $component = Livewire::test(AvailabilityResults::class, ['entry' => $this->advancedEntries->first()->id(), 'extraDays' => 1, 'extraDaysOffset' => 1]) + ->dispatch('availability-search-updated', + [ + 'dates' => [ + 'date_start' => $this->date->toISOString(), + 'date_end' => $this->date->copy()->add(2, 'day')->toISOString(), + ], + 'quantity' => 1, + 'advanced' => 'test', + 'customer' => [ + 'adults' => 2, + 'children' => 1, + ], + ] + ); + + $availability = $component->viewData('availability'); + + $component->call('checkout'); + + $this->assertDatabaseHas('resrv_reservations', + [ + 'item_id' => $this->advancedEntries->first()->id(), + 'date_start' => $this->date, + 'date_end' => $this->date->copy()->add(2, 'day'), + 'quantity' => 1, + 'property' => 'test', + 'payment' => data_get($availability[0], 'data.payment'), + 'price' => data_get($availability[0], 'data.price'), + 'customer' => json_encode(['adults' => 2, 'children' => 1]), + ] + ); + } + // Test that it return an error if the availability has changed after the results have been shown public function test_does_not_create_reservation_if_availability_changed() {