1const $ = jQuery; 2let configData = {}; 3const { klarna_interoperability } = await import("@klarna/network_session_token"); 4const params = document.getElementById("wp-script-module-data-@klarna/onsite_messaging"); 5 6if (params?.textContent) { 7 try { 8 configData = JSON.parse(params.textContent); 9 } catch {} 10} 11 12const klarna_onsite_messaging = { 13 Klarna: null, 14 placements: [], 15 params: null, 16 17 refresh: function () { 18 // Loop each placement and unmount. 19 klarna_onsite_messaging.placements.forEach((placement) => { 20 placement.unmount(); 21 }); 22 23 klarna_onsite_messaging.mount(); 24 }, 25 26 check_variable: function () { 27 if ($(".product-type-variable")[0] && $(".variation_id").val() === "0") { 28 // Product is a variable product and variable is not set 29 return false; 30 } 31 return true; 32 }, 33 34 update_total_cart: function (price) { 35 if (!price) { 36 price = Math.round($("#kosm_cart_total").val()); 37 } 38 39 if (!isNaN(price)) { 40 klarna_onsite_messaging.update_total_price(price * 100); 41 } 42 }, 43 44 update_total_variation: function (variation) { 45 let price = Math.round(variation.display_price * 100); 46 klarna_onsite_messaging.update_total_price(price); 47 }, 48 49 update_total_price: function (price) { 50 /* If the price is undefined, OSM won't appear. Let this be known. */ 51 if (!price) { 52 console.warn("OSM price error: ", price); 53 } 54 55 $("klarna-placement").each(function () { 56 $(this).attr("data-total_amount", price); 57 $(this).attr("data-purchase-amount", price); 58 }); 59 60 klarna_onsite_messaging.refresh(); 61 }, 62 63 debug_info: function () { 64 if (/[?&]osmDebug/.test(location.search)) { 65 const d = klarna_onsite_messaging.params.debug_info; 66 67 if (typeof d !== "undefined") { 68 console.log("%cDebug info: ", "color: #ff0000"); 69 for (const [key, value] of Object.entries(d)) { 70 console.log(`${key}: ${value}`); 71 } 72 73 if ( 74 typeof window.KlarnaOnsiteService !== "undefined" && 75 typeof window.KlarnaOnsiteService.loaded !== "undefined" 76 ) { 77 console.log("Klarna loaded status: " + window.KlarnaOnsiteService.loaded); 78 } 79 } 80 } 81 }, 82 83 init: async function (e) { 84 klarna_onsite_messaging.params = configData; 85 klarna_onsite_messaging.Klarna = klarna_interoperability.Klarna; 86 klarna_onsite_messaging.debug_info(); 87 88 if (klarna_onsite_messaging.check_variable) { 89 klarna_onsite_messaging.mount(); 90 } 91 92 $(document.body).on("updated_cart_totals", function () { 93 $.ajax({ 94 url: klarna_onsite_messaging.params.get_cart_total_url, 95 type: "GET", 96 dataType: "json", 97 success: function (response) { 98 console.log("success", response); 99 klarna_onsite_messaging.update_total_cart(response.data); 100 }, 101 error: function (response) { 102 console.log(response); 103 }, 104 }); 105 }); 106 107 $(document).on("found_variation", function (e, variation) { 108 klarna_onsite_messaging.update_total_variation(variation); 109 }); 110 111 /* "WooCommerce Measurement Price Calculator". */ 112 113 // Measurement with a "total price". These have more than one field/unit. 114 $(".total_price").on("wc-measurement-price-calculator-total-price-change", function (e, quantity, price) { 115 if (price) { 116 klarna_onsite_messaging.update_total_price(Math.round(quantity * price * 100)); 117 } 118 }); 119 120 // Triggered when the customer manually adjusts the quantity (rather than the measurements). 121 $("form.cart").on("wc-measurement-price-calculator-quantity-changed", function (e, quantity) { 122 if (quantity) { 123 price = 100 * quantity * parseFloat(wc_price_calculator_params.product_price); 124 klarna_onsite_messaging.update_total_price(price); 125 } 126 }); 127
128 // Measurements with a "product price". These have a single field/unit. 129 // The product price does not account for quantity, instead we have to account for this when updating OSM. 130 $(".product_price").on( 131 "wc-measurement-price-calculator-product-price-change", 132 function (e, measurement, price) { 133 quantity = parseInt($("form.cart .qty").val()); 134 if (price && quantity > 0) { 135 klarna_onsite_messaging.update_total_price(Math.round(price * 100 * quantity)); 136 } 137 }, 138 ); 139 140 $("form.cart").on("change", "input.qty", function () { 141 if (typeof wc_price_calculator_params === "undefined") { 142 return; 143 } 144 145 quantity = this.value; 146 unit_price = parseFloat($(".product_price span").text()); // NaN - if the customer has not yet entered any units in the MPC fields. 147 148 if (quantity > 0) { 149 price = 100 * quantity * parseFloat(wc_price_calculator_params.product_price); 150 if (!isNaN(unit_price)) { 151 price = 100 * unit_price * quantity; 152 } 153 154 klarna_onsite_messaging.update_total_price(price); 155 } 156 }); 157 }, 158 159 mount: async function () { 160 const placements = document.querySelectorAll("klarna-placement"); 161 162 if (!placements.length) { 163 return; 164 } 165 166 const klarnaPlacements = []; 167 for (const placement of placements) { 168 const tmpPlacement = klarna_onsite_messaging.Klarna.Messaging.placement({ 169 key: placement.dataset.key, 170 amount: parseInt(placement.dataset.purchaseAmount), 171 locale: placement.dataset.locale, 172 theme: placement.dataset.theme || "default", 173 }).mount(placement); 174 klarnaPlacements.push(tmpPlacement); 175 } 176 177 klarna_onsite_messaging.placements = klarnaPlacements; 178 }, 179}; 180 181$("body").on("klarna_wc_sdk_loaded", klarna_onsite_messaging.init); 182window.klarna_onsite_messaging = klarna_onsite_messaging;
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.