1/* 2 * Alma Frontend Widget Implementation 3 * This script initializes the Alma payment widget on the frontend. 4 * @see includes/Infrastructure/Helper/AssetsHelper.php 5 */ 6(function ($) { 7 $(function () { 8 9 /** 10 * Initialize the Alma widget for displaying payment plans. 11 */ 12 function initializeAlmaWidget(price) { 13 14 // Check settings 15 if (!alma_widget_settings) { 16 console.warn('Alma widget settings not found!'); 17 return; 18 } 19 20 // Ensure the Alma widget container is present in the DOM 21 let containerSelector = alma_widget_settings.widget_selector; 22 if ($(containerSelector).length === 0) { 23 containerSelector = alma_widget_settings.widget_default_selector; 24 } 25 26 if ($(containerSelector).length > 0) { 27 initWidget(containerSelector, price); 28 return; 29 } 30 31 // Use a MutationObserver to wait for the element to be added to the DOM 32 const observer = new MutationObserver(function (mutations, obs) { 33 const primarySelector = alma_widget_settings.widget_selector; 34 const defaultSelector = alma_widget_settings.widget_default_selector; 35 let foundSelector = null; 36 37 if ($(primarySelector).length) { 38 foundSelector = primarySelector; 39 } else if ($(defaultSelector).length) { 40 foundSelector = defaultSelector; 41 } 42 43 if (foundSelector) { 44 obs.disconnect(); // Stop observing once the element is found 45 initWidget(foundSelector, price); 46 } 47 }); 48 observer.observe(document.body, { 49 childList: true, 50 subtree: true 51 }); 52 } 53 54 /** 55 * The actual widget initialization logic. 56 */ 57 function initWidget(containerSelector, price) { 58 // Init Alma widgets 59 const widgets = Alma.Widgets.initialize( 60 alma_widget_settings.merchant_id, 61 alma_widget_settings.environment === 'live' ? Alma.ApiMode.LIVE : Alma.ApiMode.TEST, 62 ); 63 64 // Configure the widget with the provided settings 65 widgets.add(Alma.Widgets.PaymentPlans, { 66 container: containerSelector, 67 purchaseAmount: price || alma_widget_settings.price, 68 locale: alma_widget_settings.language, 69 hideIfNotEligible: alma_widget_settings.hide_if_not_eligible, 70 transitionDelay: alma_widget_settings.transition_delay, 71 monochrome: alma_widget_settings.monochrome, 72 hideBorder: alma_widget_settings.hide_border, 73 plans: alma_widget_settings.fee_plan_list 74 }); 75 } 76 77 /** 78 * Listen for changes in the cart or checkout page to reinitialize the widget. 79 */ 80 function listenToChanges() { 81 82 // Listen for changes in the cart totals to reinitialize the widget 83 $(document.body).on('updated_cart_totals', function () { 84 const price = 85 Alma.Utils.priceToCents(parseFloat($('.cart-subtotal .woocommerce-Price-amount').text())); 86 initializeAlmaWidget(price); 87 }); 88 89 // Listen for changes in the checkout page 90 $(document.body).on('found_variation', function (event, variation) { 91 const price = Alma.Utils.priceToCents(variation.display_price); 92 initializeAlmaWidget(price); 93 }); 94 } 95 96 initializeAlmaWidget(); 97 listenToChanges(); 98 }); 99})(jQuery);
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.