diff --git a/resources/views/includes/checkout/field-partial.blade.php b/resources/views/includes/checkout/field-partial.blade.php new file mode 100644 index 0000000..d2a600f --- /dev/null +++ b/resources/views/includes/checkout/field-partial.blade.php @@ -0,0 +1,4 @@ +@include($field->path, [ + 'field' => $field, + 'orderModel' => $orderModel, +]) diff --git a/resources/views/includes/checkout/field-select.blade.php b/resources/views/includes/checkout/field-select.blade.php new file mode 100644 index 0000000..2c60a7e --- /dev/null +++ b/resources/views/includes/checkout/field-select.blade.php @@ -0,0 +1,21 @@ +
has_form_error($field->getName())])> + + +
+ diff --git a/src/Livewire/Checkout.php b/src/Livewire/Checkout.php index 4362457..2fa5377 100644 --- a/src/Livewire/Checkout.php +++ b/src/Livewire/Checkout.php @@ -299,8 +299,13 @@ public function onChoosePayment($code): void $this->checkoutForm->getField('payment')->value = $code; $this->orderManager->applyCurrentPaymentFee($payment->code); - if ($this->order->payment !== $code) { - $this->order->updateQuietly(['payment' => $code]); + $cartTotal = $this->cartManager->getCart()->total(); + + if ($this->order->payment !== $code || $this->order->order_total !== $cartTotal) { + $this->order->updateQuietly([ + 'payment' => $code, + 'order_total' => $cartTotal, + ]); } $this->order = null; @@ -376,14 +381,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/src/Livewire/Concerns/SearchesNearby.php b/src/Livewire/Concerns/SearchesNearby.php index a458190..b6f9aa9 100644 --- a/src/Livewire/Concerns/SearchesNearby.php +++ b/src/Livewire/Concerns/SearchesNearby.php @@ -285,7 +285,7 @@ protected function geocodeSearchPoint($searchPoint) $userLocation = $this->handleGeocodeResponse($collection); - $this->searchQuery = $userLocation->format(); + $this->searchQuery = $userLocation->getFormattedAddress(); return $userLocation; } diff --git a/src/View/Components/AccountDashboard.php b/src/View/Components/AccountDashboard.php index b475a23..c5c9fc8 100644 --- a/src/View/Components/AccountDashboard.php +++ b/src/View/Components/AccountDashboard.php @@ -19,9 +19,9 @@ final class AccountDashboard extends Component public ?int $defaultAddressId; - public string $formattedAddress = ''; + public string $formattedAddress; - public string $customerName = ''; + public string $customerName; public function __construct() { 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(); diff --git a/tests/Livewire/LocalSearchTest.php b/tests/Livewire/LocalSearchTest.php index 1578ffb..466d71a 100644 --- a/tests/Livewire/LocalSearchTest.php +++ b/tests/Livewire/LocalSearchTest.php @@ -192,12 +192,13 @@ 'streetName' => 'Main St', 'latitude' => 51.50987615, 'longitude' => -0.1446716, + 'formattedAddress' => '123 Main St', ]), ])); Livewire::test(LocalSearch::class) ->call('onUserPositionUpdated', [51.50987615, -0.1446716]) - ->assertSet('searchQuery', '123 Main St '); + ->assertSet('searchQuery', '123 Main St'); }); it('onUpdateSearchQuery errors when no search query or point', function(): void {