@@ -29,6 +29,8 @@ public function __construct(ShippingMethodService $shippingMethodService)
29
29
*/
30
30
public function map (Quote $ quote ): array
31
31
{
32
+ $ quote ->setTotalsCollectedFlag (false );
33
+ $ quote ->collectTotals ();
32
34
$ total = $ quote ->getGrandTotal ();
33
35
$ result = [
34
36
'methodOptions ' => [
@@ -46,6 +48,21 @@ public function map(Quote $quote): array
46
48
// If methods are empty, need to choose a new address in the express sheet
47
49
if (empty ($ result ['shippingMethods ' ])) {
48
50
$ 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
+ }
49
66
}
50
67
return $ result ;
51
68
}
@@ -71,9 +88,7 @@ private function getShippingMethods(Quote $quote, bool $hasShippingAddress): ?ar
71
88
}
72
89
73
90
$ selectedMethod = $ quote ->getShippingAddress ()->getShippingMethod ();
74
- if (empty ($ selectedMethod )) {
75
- $ shippingMethods [0 ]['selected ' ] = true ;
76
- } else {
91
+ if (!empty ($ selectedMethod )) {
77
92
$ numShippingMethods = count ($ shippingMethods );
78
93
for ($ i = 0 ; $ i < $ numShippingMethods ; $ i ++) {
79
94
if ($ shippingMethods [$ i ]['id ' ] === $ selectedMethod ) {
0 commit comments