checkout.js
79 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 | const injectItemContent = (block, index) => { |
| 34 | let el = block.querySelector('.wc-block-components-product-details'); |
| 35 | |
| 36 | if (el !== null && 'ameliaNote' + index in window && !el.querySelector('.amelia-custom-html')) { |
| 37 | const div = document.createElement('div'); |
| 38 | div.className = 'amelia-custom-html'; |
| 39 | div.innerHTML = '<div>' + window['ameliaNote' + index][0] + '</div>'; |
| 40 | el.appendChild(div); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | const injectCustomContent = () => { |
| 45 | document.querySelectorAll('.wc-block-cart-items__row').forEach((block, index) => { |
| 46 | injectItemContent(block, index) |
| 47 | }); |
| 48 | |
| 49 | document.querySelectorAll('.wc-block-components-order-summary-item').forEach((block, index) => { |
| 50 | injectItemContent(block, index) |
| 51 | }); |
| 52 | }; |
| 53 | |
| 54 | injectCustomContent(); |
| 55 | |
| 56 | // Use MutationObserver to handle async block rendering |
| 57 | const observer = new MutationObserver(injectCustomContent); |
| 58 | observer.observe(document.body, { childList: true, subtree: true }); |
| 59 | } |
| 60 | }); |
| 61 | |
| 62 | const ameliaStyleTag = document.createElement('style'); |
| 63 | |
| 64 | ameliaStyleTag.innerHTML = ` |
| 65 | .wc-block-components-product-details__appointment-info { |
| 66 | display: none |
| 67 | } |
| 68 | |
| 69 | .wc-block-components-product-details__event-info { |
| 70 | display: none |
| 71 | } |
| 72 | |
| 73 | .wc-block-components-product-details__package-info { |
| 74 | display: none |
| 75 | } |
| 76 | `; |
| 77 | |
| 78 | document.head.appendChild(ameliaStyleTag); |
| 79 |