@@ -80,7 +80,7 @@ class Add
80
80
* @param ShippingAddressManagementInterface $shippingAddressManagement
81
81
* @param QuoteRepository|null $quoteRepository
82
82
* @param ShippingMethod $appleShippingMethod
83
- */
83
+ */
84
84
public function __construct (
85
85
CartRepositoryInterface $ cartRepository ,
86
86
CartInterface $ cart ,
@@ -105,16 +105,16 @@ public function __construct(
105
105
$ this ->quoteRepository = $ quoteRepository
106
106
?? ObjectManager::getInstance ()->get (QuoteRepository::class);
107
107
$ this ->appleShippingMethod = $ appleShippingMethod ;
108
-
109
108
}
110
109
111
110
public function process ($ request )
112
111
{
113
112
$ cart_hash = $ request ->getParam ('id ' );
114
113
115
- if ($ cart_hash ) {
114
+ if ($ cart_hash ) {
116
115
$ cartId = $ this ->maskedQuoteIdToQuoteId ->execute ($ cart_hash );
117
116
$ cart = $ this ->cartRepository ->get ($ cartId );
117
+
118
118
} else {
119
119
$ checkoutSession = ObjectManager::getInstance ()->get (\Magento \Checkout \Model \Session::class);
120
120
$ cart = $ checkoutSession ->getQuote ();
@@ -127,7 +127,6 @@ public function process($request)
127
127
throw new \Exception ('Product data is missing or invalid. ' );
128
128
}
129
129
130
-
131
130
$ cart ->removeAllItems ();
132
131
133
132
try {
@@ -141,7 +140,7 @@ public function process($request)
141
140
$ product ['qty ' ]
142
141
);
143
142
144
- if (isset ($ product ['selected_options ' ])) {
143
+ if (isset ($ product ['selected_options ' ])) {
145
144
$ cartItem ->setSelectedOptions ($ product ['selected_options ' ]);
146
145
}
147
146
@@ -154,6 +153,9 @@ public function process($request)
154
153
if (!$ cart ->getIsVirtual ()) {
155
154
$ shippingAddressData = $ this ->applepayModel ->processAddressFromWallet ($ wallet , 'shipping ' );
156
155
156
+ $ cart ->getShippingAddress ()->addData ($ shippingAddressData );
157
+
158
+ $ cart ->setShippingAddress ($ cart ->getShippingAddress ());
157
159
158
160
$ shippingAddress = $ this ->quoteAddressFactory ->create ();
159
161
$ shippingAddress ->addData ($ shippingAddressData );
@@ -168,27 +170,26 @@ public function process($request)
168
170
}
169
171
$ this ->quoteRepository ->save ($ cart );
170
172
//this delivery address is already assigned to the cart
171
-
172
173
try {
173
174
$ shippingMethods = $ this ->appleShippingMethod ->getAvailableMethods ($ cart );
174
175
} catch (\Exception $ e ) {
175
176
throw new \Exception (__ ('Unable to retrieve shipping methods. ' ));
176
177
}
178
+ if (count ($ shippingMethods ) == 0 ) {
179
+ return [];
177
180
178
- foreach ($ shippingMethods as $ method ) {
179
- $ shippingMethodsResult [] = [
180
- 'carrier_title ' => $ method ['carrier_title ' ],
181
- 'price_incl_tax ' => round ($ method ['amount ' ]['value ' ], 2 ),
182
- 'method_code ' => $ method ['carrier_code ' ] . '__SPLIT__ ' . $ method ['method_code ' ],
183
- 'method_title ' => $ method ['method_title ' ],
184
- ];
185
- }
181
+ } else {
182
+
183
+ foreach ($ shippingMethods as $ shippingMethod ) {
184
+ $ shippingMethodsResult [] = [
185
+ 'carrier_title ' => $ shippingMethod ->getCarrierTitle (),
186
+ 'price_incl_tax ' => round ($ shippingMethod ->getAmount (), 2 ),
187
+ 'method_code ' => $ shippingMethod ->getCarrierCode () . '_ ' . $ shippingMethod ->getMethodCode (),
188
+ 'method_title ' => $ shippingMethod ->getMethodTitle (),
189
+ ];
190
+ }
186
191
187
- if (!empty ($ shippingMethodsResult )) {
188
- // Set the first available shipping method
189
192
$ cart ->getShippingAddress ()->setShippingMethod ($ shippingMethodsResult [0 ]['method_code ' ]);
190
- } else {
191
- throw new \Exception (__ ('No shipping methods are available for the provided address. ' ));
192
193
}
193
194
}
194
195
$ cart ->setTotalsCollectedFlag (false );
@@ -198,13 +199,17 @@ public function process($request)
198
199
$ totals ['discount ' ] = round ($ cart ->getSubtotalWithDiscount () - $ cart ->getSubtotal (), 2 );
199
200
}
200
201
201
-
202
-
203
- return [
202
+ $ data = [
204
203
'shipping_methods ' => $ shippingMethodsResult ,
205
204
'totals ' => $ totals
206
205
];
206
+
207
+ $ this ->quoteRepository ->save ($ cart );
208
+ $ this ->cart ->save ();
209
+
210
+ return $ data ;
207
211
}
212
+
208
213
public function gatherTotals ($ address , $ quoteTotals )
209
214
{
210
215
$ shippingTotalInclTax = 0 ;
0 commit comments