From 1cabf45c6143b03349e91515979bc0f850040c0f Mon Sep 17 00:00:00 2001 From: "a.baraliu" Date: Wed, 4 Dec 2024 09:50:51 +0100 Subject: [PATCH] Add observer for applepay --- view/frontend/web/js/hide-applepay.js | 35 ++++++++++++++++++++------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/view/frontend/web/js/hide-applepay.js b/view/frontend/web/js/hide-applepay.js index 160b374..e994b31 100644 --- a/view/frontend/web/js/hide-applepay.js +++ b/view/frontend/web/js/hide-applepay.js @@ -1,13 +1,30 @@ -document.addEventListener('DOMContentLoaded', function() { - const checkPaySupport = function () { - if (!('ApplePaySession' in window)) return false; - if (typeof ApplePaySession === 'undefined') return false; - return true; +document.addEventListener('DOMContentLoaded', function () { + const hideApplePayIfUnsupported = function () { + const checkApplePaySupport = function () { + if (!('ApplePaySession' in window)) return false; + if (typeof ApplePaySession === 'undefined') return false; + return true; + }; + + const applePaySupported = checkApplePaySupport(); + const applePayContainer = document.getElementById('payment-method-option-buckaroo_magento2_applepay'); + if (!applePaySupported && applePayContainer) { + applePayContainer.style.display = 'none'; + } }; - const applePaySupported = checkPaySupport(); - const applePayContainer = document.getElementById('payment-method-option-buckaroo_magento2_applepay'); - if (!applePaySupported && applePayContainer) { - applePayContainer.style.display = 'none'; + hideApplePayIfUnsupported(); + + const observer = new MutationObserver(() => { + hideApplePayIfUnsupported(); + }); + + if (document.body) { + observer.observe(document.body, { + childList: true, + subtree: true + }); + } else { + console.error("document.body is not available for observation."); } });