diff --git a/src/Livewire/Checkout.php b/src/Livewire/Checkout.php index 4362457..23be3a2 100644 --- a/src/Livewire/Checkout.php +++ b/src/Livewire/Checkout.php @@ -376,14 +376,21 @@ protected function validateCheckout(Order $order) $this->withValidator(function($validator) use ($order): void { $validator->after(function($validator) use ($order): void { - if ($order->isDeliveryType() && Location::requiresUserPosition()) { - rescue(function(): void { - $this->orderManager->validateDeliveryAddress(array_only($this->fields, [ - 'address_1', 'city', 'state', 'postcode', 'country', - ])); - }, function(Throwable $ex) use ($validator): void { - $validator->errors()->add('delivery_address', $ex->getMessage()); - }); + if ($order->isDeliveryType()) { + if (Location::requiresUserPosition()) { + rescue(function(): void { + $this->orderManager->validateDeliveryAddress(array_only($this->fields, [ + 'address_1', 'city', 'state', 'postcode', 'country', + ])); + }, function(Throwable $ex) use ($validator): void { + $validator->errors()->add('delivery_address', $ex->getMessage()); + }); + } elseif (blank(array_get($this->fields, 'address_1'))) { + // The delivery area restriction is disabled, so the address is + // deliberately not geocoded or area-checked. It must still exist: + // a delivery order without a street address cannot be delivered. + $validator->errors()->add('delivery_address', lang('igniter.local::default.alert_missing_street_address')); + } } if ($this->fields['payment'] && !$this->orderManager->getPayment($this->fields['payment'])) { diff --git a/tests/Livewire/CheckoutTest.php b/tests/Livewire/CheckoutTest.php index 720aaca..9d0fc79 100644 --- a/tests/Livewire/CheckoutTest.php +++ b/tests/Livewire/CheckoutTest.php @@ -277,6 +277,39 @@ function setupCheckout() ->assertHasErrors(['delivery_address' => [lang('igniter.local::default.alert_missing_street_address')]]); }); +it('onValidate requires a delivery address when the delivery area restriction is disabled', function(): void { + setting()->set(['location_order' => '0']); + setupCheckout(); + + // No usable position, so no address is prepared onto the order + LocationFacade::updateUserPosition(new GeoliteLocation('test')); + + $order = resolve(OrderManager::class)->getOrder(); + $order->order_type = Location::DELIVERY; + $order->save(); + LocationFacade::updateOrderType(Location::DELIVERY); + + Livewire::test(Checkout::class) + ->dispatch('checkout::validate') + ->assertHasErrors(['delivery_address' => [lang('igniter.local::default.alert_missing_street_address')]]); +}); + +it('onValidate does not geocode the delivery address when the delivery area restriction is disabled', function(): void { + setting()->set(['location_order' => '0']); + setupCheckout(); + + $order = resolve(OrderManager::class)->getOrder(); + $order->order_type = Location::DELIVERY; + $order->save(); + LocationFacade::updateOrderType(Location::DELIVERY); + + Geocoder::shouldReceive('geocode')->never(); + + Livewire::test(Checkout::class) + ->dispatch('checkout::validate') + ->assertHasNoErrors('delivery_address'); +}); + it('onValidate dispatches an event on success', function(): void { Event::fake(['igniter.orange.validateCheckout']); setupCheckout();