Skip to content

Commit fe0adc7

Browse files
Merge pull request #1125 from buckaroo-it/BP-3885-Apple-Pay-transaction-screen-is-disappearing-on-the-product-page
Bp 3885 apple pay transaction screen is disappearing on the product page
2 parents 7405f18 + ec7d1d3 commit fe0adc7

File tree

3 files changed

+35
-34
lines changed

3 files changed

+35
-34
lines changed

Model/Applepay.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,4 @@ public function processAddressFromWallet($wallet, $type = 'shipping')
3131

3232
return $address;
3333
}
34-
}
34+
}

Service/Applepay/Add.php

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class Add
8080
* @param ShippingAddressManagementInterface $shippingAddressManagement
8181
* @param QuoteRepository|null $quoteRepository
8282
* @param ShippingMethod $appleShippingMethod
83-
*/
83+
*/
8484
public function __construct(
8585
CartRepositoryInterface $cartRepository,
8686
CartInterface $cart,
@@ -105,16 +105,16 @@ public function __construct(
105105
$this->quoteRepository = $quoteRepository
106106
?? ObjectManager::getInstance()->get(QuoteRepository::class);
107107
$this->appleShippingMethod = $appleShippingMethod;
108-
109108
}
110109

111110
public function process($request)
112111
{
113112
$cart_hash = $request->getParam('id');
114113

115-
if($cart_hash) {
114+
if ($cart_hash) {
116115
$cartId = $this->maskedQuoteIdToQuoteId->execute($cart_hash);
117116
$cart = $this->cartRepository->get($cartId);
117+
118118
} else {
119119
$checkoutSession = ObjectManager::getInstance()->get(\Magento\Checkout\Model\Session::class);
120120
$cart = $checkoutSession->getQuote();
@@ -127,7 +127,6 @@ public function process($request)
127127
throw new \Exception('Product data is missing or invalid.');
128128
}
129129

130-
131130
$cart->removeAllItems();
132131

133132
try {
@@ -141,7 +140,7 @@ public function process($request)
141140
$product['qty']
142141
);
143142

144-
if(isset($product['selected_options'])) {
143+
if (isset($product['selected_options'])) {
145144
$cartItem->setSelectedOptions($product['selected_options']);
146145
}
147146

@@ -154,6 +153,9 @@ public function process($request)
154153
if (!$cart->getIsVirtual()) {
155154
$shippingAddressData = $this->applepayModel->processAddressFromWallet($wallet, 'shipping');
156155

156+
$cart->getShippingAddress()->addData($shippingAddressData);
157+
158+
$cart->setShippingAddress($cart->getShippingAddress());
157159

158160
$shippingAddress = $this->quoteAddressFactory->create();
159161
$shippingAddress->addData($shippingAddressData);
@@ -168,27 +170,26 @@ public function process($request)
168170
}
169171
$this->quoteRepository->save($cart);
170172
//this delivery address is already assigned to the cart
171-
172173
try {
173174
$shippingMethods = $this->appleShippingMethod->getAvailableMethods($cart);
174175
} catch (\Exception $e) {
175176
throw new \Exception(__('Unable to retrieve shipping methods.'));
176177
}
178+
if (count($shippingMethods) == 0) {
179+
return [];
177180

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+
}
186191

187-
if (!empty($shippingMethodsResult)) {
188-
// Set the first available shipping method
189192
$cart->getShippingAddress()->setShippingMethod($shippingMethodsResult[0]['method_code']);
190-
} else {
191-
throw new \Exception(__('No shipping methods are available for the provided address.'));
192193
}
193194
}
194195
$cart->setTotalsCollectedFlag(false);
@@ -198,13 +199,17 @@ public function process($request)
198199
$totals['discount'] = round($cart->getSubtotalWithDiscount() - $cart->getSubtotal(), 2);
199200
}
200201

201-
202-
203-
return [
202+
$data = [
204203
'shipping_methods' => $shippingMethodsResult,
205204
'totals' => $totals
206205
];
206+
207+
$this->quoteRepository->save($cart);
208+
$this->cart->save();
209+
210+
return $data;
207211
}
212+
208213
public function gatherTotals($address, $quoteTotals)
209214
{
210215
$shippingTotalInclTax = 0;

Service/Applepay/ShippingMethod.php

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,32 @@ public function __construct(
3636
ExtensibleDataObjectConverter $dataObjectConverter,
3737
ShippingMethodConverter $shippingMethodConverter,
3838
TotalsCollector $totalsCollector
39+
3940
) {
4041
$this->dataObjectConverter = $dataObjectConverter;
4142
$this->shippingMethodConverter = $shippingMethodConverter;
4243
$this->totalsCollector = $totalsCollector;
44+
4345
}
4446

4547
public function getAvailableMethods($cart)
4648
{
4749
$address = $cart->getShippingAddress();
48-
4950
$address->setLimitCarrier(null);
51+
5052
$address->setQuote($cart);
5153
$address->setCollectShippingRates(true);
5254
$this->totalsCollector->collectAddressTotals($cart, $address);
53-
$methods = [];
5455

56+
$methods = [];
5557
$shippingRates = $address->getGroupedAllShippingRates();
58+
5659
foreach ($shippingRates as $carrierRates) {
5760
foreach ($carrierRates as $rate) {
58-
$methodData = $this->dataObjectConverter->toFlatArray(
59-
$this->shippingMethodConverter->modelToDataObject($rate, $cart->getQuoteCurrencyCode()),
60-
[],
61-
ShippingMethodInterface::class
62-
);
63-
$methods[] = $this->processMoneyTypeData(
64-
$methodData,
65-
$cart->getQuoteCurrencyCode()
66-
);
61+
$methods[] = $this->shippingMethodConverter->modelToDataObject($rate, $cart->getQuoteCurrencyCode());
6762
}
6863
}
64+
6965
return $methods;
7066
}
7167
private function processMoneyTypeData(array $data, string $quoteCurrencyCode): array
@@ -86,4 +82,4 @@ private function processMoneyTypeData(array $data, string $quoteCurrencyCode): a
8682
}
8783
return $data;
8884
}
89-
}
85+
}

0 commit comments

Comments
 (0)