You've already forked wc-licensed-product
Fix licenses not showing in admin order form for variable products (v0.5.13)
- Fix OrderLicenseController to use isLicensedProduct() for consistent product type detection - Fixed expected licenses calculation for variable product orders - Fixed manual license generation from admin order page for variable products - Remove debug logging from all source files (PHP and JavaScript) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -18,12 +18,25 @@
|
||||
}
|
||||
|
||||
const { getSetting } = wc.wcSettings;
|
||||
const { createElement, useState } = wp.element;
|
||||
const { createElement, useState, useEffect, useCallback } = wp.element;
|
||||
const { TextControl } = wp.components;
|
||||
const { __ } = wp.i18n;
|
||||
|
||||
// Get available exports from blocksCheckout
|
||||
const { ExperimentalOrderMeta } = wc.blocksCheckout;
|
||||
const { ExperimentalOrderMeta, extensionCartUpdate } = wc.blocksCheckout;
|
||||
|
||||
// Debounce function for API updates
|
||||
function debounce(func, wait) {
|
||||
let timeout;
|
||||
return function executedFunction(...args) {
|
||||
const later = () => {
|
||||
clearTimeout(timeout);
|
||||
func(...args);
|
||||
};
|
||||
clearTimeout(timeout);
|
||||
timeout = setTimeout(later, wait);
|
||||
};
|
||||
}
|
||||
|
||||
// Get settings from PHP
|
||||
const settings = getSetting('wc-licensed-product_data', {});
|
||||
@@ -59,6 +72,23 @@
|
||||
const [domain, setDomain] = useState('');
|
||||
const [error, setError] = useState('');
|
||||
|
||||
// Debounced API update function
|
||||
const updateStoreApi = useCallback(
|
||||
debounce((normalizedDomain) => {
|
||||
if (extensionCartUpdate) {
|
||||
extensionCartUpdate({
|
||||
namespace: 'wc-licensed-product',
|
||||
data: {
|
||||
licensed_product_domain: normalizedDomain,
|
||||
},
|
||||
}).catch(err => {
|
||||
console.error('[WCLP] Store API update error:', err);
|
||||
});
|
||||
}
|
||||
}, 500),
|
||||
[]
|
||||
);
|
||||
|
||||
const handleChange = (value) => {
|
||||
const normalized = normalizeDomain(value);
|
||||
setDomain(normalized);
|
||||
@@ -67,9 +97,11 @@
|
||||
setError(settings.validationError || __('Please enter a valid domain.', 'wc-licensed-product'));
|
||||
} else {
|
||||
setError('');
|
||||
// Update Store API when valid
|
||||
updateStoreApi(normalized);
|
||||
}
|
||||
|
||||
// Store in hidden input for form submission
|
||||
// Store in hidden input for form submission (fallback)
|
||||
const hiddenInput = document.getElementById('wclp-domain-hidden');
|
||||
if (hiddenInput) {
|
||||
hiddenInput.value = normalized;
|
||||
@@ -135,6 +167,23 @@
|
||||
});
|
||||
const [errors, setErrors] = useState({});
|
||||
|
||||
// Debounced API update function
|
||||
const updateStoreApi = useCallback(
|
||||
debounce((domainsData) => {
|
||||
if (extensionCartUpdate) {
|
||||
extensionCartUpdate({
|
||||
namespace: 'wc-licensed-product',
|
||||
data: {
|
||||
licensed_product_domains: domainsData,
|
||||
},
|
||||
}).catch(err => {
|
||||
console.error('[WCLP] Store API update error:', err);
|
||||
});
|
||||
}
|
||||
}, 500),
|
||||
[]
|
||||
);
|
||||
|
||||
if (!products.length) {
|
||||
return null;
|
||||
}
|
||||
@@ -174,7 +223,7 @@
|
||||
|
||||
setErrors(newErrors);
|
||||
|
||||
// Update hidden field with variation support
|
||||
// Build domain data for Store API
|
||||
const data = products.map(p => {
|
||||
const pKey = getProductKey(p);
|
||||
const doms = newDomains[pKey] || [];
|
||||
@@ -188,6 +237,10 @@
|
||||
return entry;
|
||||
}).filter(item => item.domains.length > 0);
|
||||
|
||||
// Update Store API
|
||||
updateStoreApi(data);
|
||||
|
||||
// Update hidden field (fallback)
|
||||
const hiddenInput = document.getElementById('wclp-domains-hidden');
|
||||
if (hiddenInput) {
|
||||
hiddenInput.value = JSON.stringify(data);
|
||||
@@ -273,11 +326,13 @@
|
||||
|
||||
if (registerPlugin) {
|
||||
registerPlugin('wc-licensed-product-domain-fields', {
|
||||
render: () => createElement(
|
||||
ExperimentalOrderMeta,
|
||||
{},
|
||||
createElement(LicenseDomainsBlock)
|
||||
),
|
||||
render: () => {
|
||||
return createElement(
|
||||
ExperimentalOrderMeta,
|
||||
{},
|
||||
createElement(LicenseDomainsBlock)
|
||||
);
|
||||
},
|
||||
scope: 'woocommerce-checkout',
|
||||
});
|
||||
}
|
||||
@@ -379,6 +434,68 @@
|
||||
} else {
|
||||
insertionPoint.appendChild(container);
|
||||
}
|
||||
|
||||
// Add event listeners to sync with Store API
|
||||
const debouncedUpdate = debounce(function() {
|
||||
if (!extensionCartUpdate) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (settings.isMultiDomainEnabled && settings.licensedProducts) {
|
||||
// Collect multi-domain data
|
||||
const domainsData = settings.licensedProducts.map(function(product) {
|
||||
const productKey = product.variation_id && product.variation_id > 0
|
||||
? product.product_id + '_' + product.variation_id
|
||||
: String(product.product_id);
|
||||
|
||||
const domains = [];
|
||||
for (let i = 0; i < product.quantity; i++) {
|
||||
const input = container.querySelector('input[name="licensed_domains[' + productKey + '][' + i + ']"]');
|
||||
if (input && input.value.trim()) {
|
||||
domains.push(normalizeDomain(input.value));
|
||||
}
|
||||
}
|
||||
|
||||
const entry = {
|
||||
product_id: product.product_id,
|
||||
domains: domains,
|
||||
};
|
||||
if (product.variation_id && product.variation_id > 0) {
|
||||
entry.variation_id = product.variation_id;
|
||||
}
|
||||
return entry;
|
||||
}).filter(function(item) { return item.domains.length > 0; });
|
||||
|
||||
extensionCartUpdate({
|
||||
namespace: 'wc-licensed-product',
|
||||
data: {
|
||||
licensed_product_domains: domainsData,
|
||||
},
|
||||
}).catch(function(err) {
|
||||
console.error('[WCLP] Store API update error:', err);
|
||||
});
|
||||
} else {
|
||||
// Single domain
|
||||
const input = container.querySelector('input[name="licensed_product_domain"]');
|
||||
if (input) {
|
||||
const domain = normalizeDomain(input.value);
|
||||
extensionCartUpdate({
|
||||
namespace: 'wc-licensed-product',
|
||||
data: {
|
||||
licensed_product_domain: domain,
|
||||
},
|
||||
}).catch(function(err) {
|
||||
console.error('[WCLP] Store API update error:', err);
|
||||
});
|
||||
}
|
||||
}
|
||||
}, 500);
|
||||
|
||||
// Attach event listeners to all domain inputs
|
||||
container.querySelectorAll('input[type="text"]').forEach(function(input) {
|
||||
input.addEventListener('input', debouncedUpdate);
|
||||
input.addEventListener('change', debouncedUpdate);
|
||||
});
|
||||
}, 2000);
|
||||
|
||||
})();
|
||||
|
||||
Reference in New Issue
Block a user