checkout.js
53 lines
| 1 | function setAmeliaFieldValue(selector, value) { |
| 2 | let element = document.querySelector(selector); |
| 3 | |
| 4 | if (typeof element !== 'undefined' && element) { |
| 5 | const valueSetter = Object.getOwnPropertyDescriptor(element, 'value').set; |
| 6 | const prototype = Object.getPrototypeOf(element); |
| 7 | const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set; |
| 8 | |
| 9 | if (valueSetter && valueSetter !== prototypeValueSetter) { |
| 10 | prototypeValueSetter.call(element, value); |
| 11 | } else { |
| 12 | valueSetter.call(element, value); |
| 13 | } |
| 14 | |
| 15 | element.dispatchEvent(new Event('input', { bubbles: true })); |
| 16 | } |
| 17 | } |
| 18 | |
| 19 | document.addEventListener('DOMContentLoaded', function() { |
| 20 | if ('ameliaCustomer' in window) { |
| 21 | let ameliaCustomerInterval = setInterval( |
| 22 | function () { |
| 23 | if (document.body.classList.contains('woocommerce-checkout')) { |
| 24 | clearInterval(ameliaCustomerInterval); |
| 25 | |
| 26 | Object.keys(ameliaCustomer).forEach((key) => { |
| 27 | setAmeliaFieldValue('#' + key, ameliaCustomer[key]); |
| 28 | }) |
| 29 | } |
| 30 | }, 500 |
| 31 | ) |
| 32 | } |
| 33 | |
| 34 | if ('ameliaNote' in window && window.ameliaNote.length) { |
| 35 | const injectCustomContent = () => { |
| 36 | document.querySelectorAll('.wc-block-components-product-details').forEach((el) => { |
| 37 | if (!el.querySelector('.amelia-custom-html')) { |
| 38 | const div = document.createElement('div'); |
| 39 | div.className = 'amelia-custom-html'; |
| 40 | div.innerHTML = '<div>' + window.ameliaNote[0] + '</div>'; |
| 41 | el.appendChild(div); |
| 42 | } |
| 43 | }); |
| 44 | }; |
| 45 | |
| 46 | injectCustomContent(); |
| 47 | |
| 48 | // Use MutationObserver to handle async block rendering |
| 49 | const observer = new MutationObserver(injectCustomContent); |
| 50 | observer.observe(document.body, { childList: true, subtree: true }); |
| 51 | } |
| 52 | }); |
| 53 |