Skip to content

Commit beb7fa6

Browse files
Force user to confirm shipping address when shipping method is not selected but address is prefilled (#282)
* Force user to confirm shipping address when shipping method is not selected but address is prefilled * Calculate totals before mapping
1 parent 01591db commit beb7fa6

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/Service/Express/ExpressPaymentRequestMapper.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function __construct(ShippingMethodService $shippingMethodService)
2929
*/
3030
public function map(Quote $quote): array
3131
{
32+
$quote->setTotalsCollectedFlag(false);
33+
$quote->collectTotals();
3234
$total = $quote->getGrandTotal();
3335
$result = [
3436
'methodOptions' => [
@@ -46,6 +48,21 @@ public function map(Quote $quote): array
4648
// If methods are empty, need to choose a new address in the express sheet
4749
if (empty($result['shippingMethods'])) {
4850
$result['shipping'] = null;
51+
} else {
52+
/*
53+
If the user didn't pick a shipping method, drop the shipping postcode. This forces the user to confirm their
54+
shipping address in the Apple Pay sheet. Which in turn sends the appropriate events from apple to the
55+
frontend, and we can keep the quotes in sync at this point.
56+
57+
We also can't just default to the first method ourselves since that would force a preselection on page load.
58+
Also, no heavy work can run on Apple Pay button click — the browser only allows direct user actions there
59+
*/
60+
$hasSelected = in_array(true, array_column($result['shippingMethods'], 'selected'), true);
61+
if (!$hasSelected) {
62+
if (isset($result['shipping']['address'])) {
63+
$result['shipping']['address']['postcode'] = null;
64+
}
65+
}
4966
}
5067
return $result;
5168
}
@@ -71,9 +88,7 @@ private function getShippingMethods(Quote $quote, bool $hasShippingAddress): ?ar
7188
}
7289

7390
$selectedMethod = $quote->getShippingAddress()->getShippingMethod();
74-
if (empty($selectedMethod)) {
75-
$shippingMethods[0]['selected'] = true;
76-
} else {
91+
if (!empty($selectedMethod)) {
7792
$numShippingMethods = count($shippingMethods);
7893
for ($i = 0; $i < $numShippingMethods; $i++) {
7994
if ($shippingMethods[$i]['id'] === $selectedMethod) {

0 commit comments

Comments
 (0)