diff --git a/Block/Catalog/Product/View/Applepay.php b/Block/Catalog/Product/View/Applepay.php index 9870b9d79..5c7bbae2c 100644 --- a/Block/Catalog/Product/View/Applepay.php +++ b/Block/Catalog/Product/View/Applepay.php @@ -62,8 +62,7 @@ public function __construct( */ public function canShowButton($page) { - return $this->cart->getSummaryQty() && - $this->isModuleActive() && + return $this->isModuleActive() && in_array($page, $this->applepayConfigProvider->getAvailableButtons()) && $this->applepayConfigProvider->isApplePayEnabled($this->_storeManager->getStore()); } diff --git a/Service/Applepay/Add.php b/Service/Applepay/Add.php index 7b8ddd232..f9fa4263a 100644 --- a/Service/Applepay/Add.php +++ b/Service/Applepay/Add.php @@ -111,7 +111,7 @@ public function __construct( public function process($request) { $cart_hash = $request->getParam('id'); - + if($cart_hash) { $cartId = $this->maskedQuoteIdToQuoteId->execute($cart_hash); $cart = $this->cartRepository->get($cartId); @@ -121,14 +121,21 @@ public function process($request) } $product = $request->getParam('product'); + + // Check if product data is present and valid + if (!$product || !is_array($product) || !isset($product['id']) || !is_numeric($product['id'])) { + throw new \Exception('Product data is missing or invalid.'); + } + + $cart->removeAllItems(); - + try { $productToBeAdded = $this->productRepository->getById($product['id']); } catch (NoSuchEntityException $e) { throw new NoSuchEntityException(__('Could not find a product with ID "%id"', ['id' => $product['id']])); } - + $cartItem = new CartItem( $productToBeAdded->getSku(), $product['qty'] @@ -140,19 +147,19 @@ public function process($request) $cart->addProduct($productToBeAdded, $this->requestBuilder->build($cartItem)); $this->cartRepository->save($cart); - + $wallet = $request->getParam('wallet'); $shippingMethodsResult = []; if (!$cart->getIsVirtual()) { $shippingAddressData = $this->applepayModel->processAddressFromWallet($wallet, 'shipping'); - - + + $shippingAddress = $this->quoteAddressFactory->create(); $shippingAddress->addData($shippingAddressData); - $errors = $shippingAddress->validate(); - + $errors = $shippingAddress->validate(); + try { $this->shippingAddressManagement->assign($cart->getId(), $shippingAddress); } catch (\Exception $e) { @@ -161,16 +168,28 @@ public function process($request) } $this->quoteRepository->save($cart); //this delivery address is already assigned to the cart - $shippingMethods = $this->appleShippingMethod->getAvailableMethods( $cart); - foreach ($shippingMethods as $index => $shippingMethod) { + + try { + $shippingMethods = $this->appleShippingMethod->getAvailableMethods($cart); + } catch (\Exception $e) { + throw new \Exception(__('Unable to retrieve shipping methods.')); + } + + foreach ($shippingMethods as $method) { $shippingMethodsResult[] = [ - 'carrier_title' => $shippingMethod['carrier_title'], - 'price_incl_tax' => round($shippingMethod['amount'], 2), - 'method_code' => $shippingMethod['carrier_code'] . '_' . $shippingMethod['method_code'], - 'method_title' => $shippingMethod['method_title'], + 'carrier_title' => $method['carrier_title'], + 'price_incl_tax' => round($method['amount']['value'], 2), + 'method_code' => $method['carrier_code'] . '__SPLIT__' . $method['method_code'], + 'method_title' => $method['method_title'], ]; } - $cart->getShippingAddress()->setShippingMethod($shippingMethodsResult[0]['method_code']); + + if (!empty($shippingMethodsResult)) { + // Set the first available shipping method + $cart->getShippingAddress()->setShippingMethod($shippingMethodsResult[0]['method_code']); + } else { + throw new \Exception(__('No shipping methods are available for the provided address.')); + } } $cart->setTotalsCollectedFlag(false); $cart->collectTotals(); @@ -200,4 +219,4 @@ public function gatherTotals($address, $quoteTotals) 'grand_total' => $quoteTotals['grand_total']->getValue() ]; } -} \ No newline at end of file +} diff --git a/view/frontend/templates/catalog/product/view/applepay.phtml b/view/frontend/templates/catalog/product/view/applepay.phtml index 8be785333..47759c26d 100644 --- a/view/frontend/templates/catalog/product/view/applepay.phtml +++ b/view/frontend/templates/catalog/product/view/applepay.phtml @@ -19,8 +19,8 @@ */ ?> canShowButton('Product')): ?> -
- +
+ diff --git a/view/frontend/web/js/view/checkout/applepay/checkout-cart.js b/view/frontend/web/js/view/checkout/applepay/checkout-cart.js index 42e026c20..8823a1989 100644 --- a/view/frontend/web/js/view/checkout/applepay/checkout-cart.js +++ b/view/frontend/web/js/view/checkout/applepay/checkout-cart.js @@ -32,9 +32,9 @@ define( 'use strict'; return { - showPayButton: function () { + showPayButton: function ($page) { applepayPay.setQuote(quote); - applepayPay.showPayButton('cart'); + applepayPay.showPayButton($page); applepayPay.transactionResult.subscribe( function () { diff --git a/view/frontend/web/js/view/checkout/applepay/pay.js b/view/frontend/web/js/view/checkout/applepay/pay.js index 700db28ca..ae4efa01f 100644 --- a/view/frontend/web/js/view/checkout/applepay/pay.js +++ b/view/frontend/web/js/view/checkout/applepay/pay.js @@ -392,9 +392,9 @@ define( } }.bind(this), }) - .fail(function() { - this.timeoutRedirect(); - }.bind(this)); + .fail(function() { + this.timeoutRedirect(); + }.bind(this)); return update; } else if (this.payMode == 'cart') { @@ -432,9 +432,9 @@ define( } }.bind(this), }) - .fail(function() { - this.timeoutRedirect(); - }.bind(this)); + .fail(function() { + this.timeoutRedirect(); + }.bind(this)); return update; } else { @@ -548,9 +548,9 @@ define( } }.bind(this), }) - .fail(function() { - this.timeoutRedirect(); - }.bind(this)); + .fail(function() { + this.timeoutRedirect(); + }.bind(this)); return update; @@ -607,10 +607,22 @@ define( initProductViewWatchers: function () { this.devLog('==============applepaydebug/initProductViewWatchers'); - this.productSelected.id = $('.price-box').attr('data-product-id'); - this.productSelected.qty = $('#qty').val(); - var self = this; + var productId = $('.price-box').attr('data-product-id'); + var productQty = $('#qty').val(); + + if (!productId) { + console.error('Product ID not found on the page.'); + return; + } + + if (!productQty) { + productQty = 1; + } + this.productSelected.id = productId; + this.productSelected.qty = productQty; + + var self = this; $('#qty').change(function() { self.productSelected.qty = $(this).val(); });