| |
| <?php
|
|
|
|
|
|
|
|
|
| add_action( 'wp_footer', 'wwof_v3_antdesign_qty_checkbox_script_final', 999 );
|
| function wwof_v3_antdesign_qty_checkbox_script_final() {
|
| ?>
|
| <script type="text/javascript">
|
| document.addEventListener('DOMContentLoaded', function() {
|
|
|
|
|
| function setNativeValue(element, value) {
|
| const valueSetter = Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype, 'value').set;
|
| if (valueSetter) {
|
| valueSetter.call(element, value);
|
| } else {
|
| element.value = value;
|
| }
|
| element.dispatchEvent(new Event('input', { bubbles: true }));
|
| }
|
|
|
|
|
| const observer = new MutationObserver(function(mutations) {
|
| mutations.forEach(function(mutation) {
|
| if (mutation.addedNodes.length) {
|
| mutation.addedNodes.forEach(function(node) {
|
| if (node.nodeType === 1) {
|
| const qtyInputs = node.querySelectorAll ? node.querySelectorAll('.wwof-row .ant-input-number-input') : [];
|
|
|
| qtyInputs.forEach(function(input) {
|
| if (input.getAttribute('data-wwof-fixed') !== 'true') {
|
| input.setAttribute('data-wwof-fixed', 'true');
|
|
|
|
|
| input.setAttribute('aria-valuemin', '0');
|
| input.setAttribute('min', '0');
|
|
|
|
|
| setTimeout(function() {
|
| setNativeValue(input, '0');
|
| }, 50);
|
| }
|
| });
|
| }
|
| });
|
| }
|
| });
|
| });
|
|
|
|
|
| observer.observe(document.body, { childList: true, subtree: true });
|
|
|
|
|
| function syncRowCheckbox(row) {
|
| if (!row) return;
|
|
|
| const input = row.querySelector('.ant-input-number-input');
|
| const checkbox = row.querySelector('.add-to-cart-checkbox input[type="checkbox"]');
|
|
|
| if (!input || !checkbox) return;
|
|
|
| const val = parseFloat(input.value);
|
| const qty = isNaN(val) ? 0 : val;
|
| const isChecked = checkbox.checked;
|
|
|
|
|
| if (qty > 0 && !isChecked) {
|
| checkbox.click();
|
| } else if (qty <= 0 && isChecked) {
|
| checkbox.click();
|
| }
|
| }
|
|
|
|
|
| document.body.addEventListener('input', function(e) {
|
| if (e.target.matches('.wwof-row .ant-input-number-input')) {
|
| syncRowCheckbox(e.target.closest('.wwof-row'));
|
| }
|
| });
|
|
|
|
|
| document.body.addEventListener('click', function(e) {
|
|
|
| const arrowHandler = e.target.closest('.ant-input-number-handler');
|
| if (arrowHandler) {
|
| const row = arrowHandler.closest('.wwof-row');
|
| if (row) {
|
|
|
| setTimeout(function() {
|
| syncRowCheckbox(row);
|
| }, 50);
|
| }
|
| }
|
| });
|
|
|
| });
|
| </script>
|
| <?php
|
| }
|
|
|
|
|
| add_filter( 'woocommerce_quantity_input_min', '__return_zero', 999 );
|
| |
| |
Comments