You've already forked wc-licensed-product
Fix Product Versions meta box not appearing for licensed-variable products (v0.5.14)
- Product Versions meta box now always added to product pages, visibility controlled via CSS/JavaScript - Added Installer::registerProductTypes() to create product type terms in the product_type taxonomy - Product type terms are now ensured to exist on woocommerce_init hook for existing installations - Fixed License Settings tab and Product Versions visibility toggling when changing product types Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
@@ -30,6 +30,9 @@ final class LicensedProductType
|
||||
*/
|
||||
private function registerHooks(): void
|
||||
{
|
||||
// Ensure product type terms exist in taxonomy (for WC_Product_Factory::get_product_type())
|
||||
add_action('woocommerce_init', [$this, 'ensureProductTypeTermsExist']);
|
||||
|
||||
// Register product types
|
||||
add_filter('product_type_selector', [$this, 'addProductType']);
|
||||
add_filter('woocommerce_product_class', [$this, 'getProductClass'], 10, 4);
|
||||
@@ -74,6 +77,15 @@ final class LicensedProductType
|
||||
add_action('admin_footer', [$this, 'addVariableProductScripts']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure product type terms exist in the product_type taxonomy
|
||||
* This is required for WC_Product_Factory::get_product_type() to work correctly
|
||||
*/
|
||||
public function ensureProductTypeTermsExist(): void
|
||||
{
|
||||
\Jeremias\WcLicensedProduct\Installer::registerProductTypes();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add product types to selector
|
||||
*/
|
||||
@@ -227,35 +239,6 @@ final class LicensedProductType
|
||||
?>
|
||||
</div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
jQuery(document).ready(function($) {
|
||||
// Show/hide panels based on product type for license settings tab
|
||||
function toggleLicensedProductOptions() {
|
||||
var productType = $('#product-type').val();
|
||||
var isLicensed = productType === 'licensed';
|
||||
var isLicensedVariable = productType === 'licensed-variable';
|
||||
|
||||
if (isLicensed || isLicensedVariable) {
|
||||
// Show license settings tab
|
||||
$('.show_if_licensed').show();
|
||||
$('.show_if_licensed-variable').show();
|
||||
$('.general_options').show();
|
||||
$('.pricing').show();
|
||||
$('.general_tab').show();
|
||||
} else {
|
||||
// Hide license settings tab for other product types
|
||||
$('.show_if_licensed').hide();
|
||||
$('.show_if_licensed-variable').hide();
|
||||
}
|
||||
}
|
||||
|
||||
// Initial state on page load
|
||||
toggleLicensedProductOptions();
|
||||
|
||||
// On product type change
|
||||
$('#product-type').on('change', toggleLicensedProductOptions);
|
||||
});
|
||||
</script>
|
||||
<?php
|
||||
}
|
||||
|
||||
@@ -642,7 +625,8 @@ final class LicensedProductType
|
||||
}
|
||||
|
||||
/**
|
||||
* Add JavaScript for licensed-variable product type in admin
|
||||
* Add JavaScript for licensed product types in admin
|
||||
* Handles visibility of License Settings tab and Product Versions meta box
|
||||
*/
|
||||
public function addVariableProductScripts(): void
|
||||
{
|
||||
@@ -659,21 +643,39 @@ final class LicensedProductType
|
||||
?>
|
||||
<script type="text/javascript">
|
||||
jQuery(document).ready(function($) {
|
||||
// Show/hide panels based on product type
|
||||
function toggleLicensedVariableOptions() {
|
||||
// Main function to handle all licensed product type visibility
|
||||
function toggleLicensedProductVisibility() {
|
||||
var productType = $('#product-type').val();
|
||||
var isLicensed = productType === 'licensed';
|
||||
var isLicensedVariable = productType === 'licensed-variable';
|
||||
|
||||
if (productType === 'licensed-variable') {
|
||||
// Handle License Settings tab visibility using class toggle (not show/hide)
|
||||
// This preserves proper CSS display values for tab list items
|
||||
if (isLicensed || isLicensedVariable) {
|
||||
$('.show_if_licensed').addClass('wclp-active');
|
||||
$('.show_if_licensed-variable').addClass('wclp-active');
|
||||
$('.general_options').show();
|
||||
$('.pricing').show();
|
||||
$('.general_tab').show();
|
||||
} else {
|
||||
$('.show_if_licensed').removeClass('wclp-active');
|
||||
$('.show_if_licensed-variable').removeClass('wclp-active');
|
||||
}
|
||||
|
||||
// Handle Product Versions meta box visibility
|
||||
if (isLicensed || isLicensedVariable) {
|
||||
$('#wc_licensed_product_versions').show();
|
||||
} else {
|
||||
$('#wc_licensed_product_versions').hide();
|
||||
}
|
||||
|
||||
// Handle licensed-variable specific options
|
||||
if (isLicensedVariable) {
|
||||
// Show variable product options
|
||||
$('.show_if_variable').show();
|
||||
$('.hide_if_variable').hide();
|
||||
|
||||
// Show licensed product options
|
||||
$('.show_if_licensed-variable').show();
|
||||
$('.show_if_licensed').show();
|
||||
|
||||
// Show general and variations tabs
|
||||
$('.general_tab').show();
|
||||
// Show variations tab
|
||||
$('.variations_tab').show();
|
||||
$('.variations_options').show();
|
||||
|
||||
@@ -685,34 +687,33 @@ final class LicensedProductType
|
||||
}
|
||||
}
|
||||
|
||||
// Initial check
|
||||
toggleLicensedVariableOptions();
|
||||
// Initial check on page load
|
||||
toggleLicensedProductVisibility();
|
||||
|
||||
// On product type change
|
||||
$('#product-type').on('change', function() {
|
||||
// Use setTimeout to let WooCommerce finish its own processing first
|
||||
setTimeout(toggleLicensedVariableOptions, 100);
|
||||
setTimeout(toggleLicensedProductVisibility, 100);
|
||||
});
|
||||
|
||||
// Re-apply after WooCommerce AJAX operations that may reset visibility
|
||||
$(document).on('woocommerce_variations_loaded', toggleLicensedVariableOptions);
|
||||
$(document).on('woocommerce_variations_added', toggleLicensedVariableOptions);
|
||||
$(document).on('woocommerce_variations_saved', toggleLicensedVariableOptions);
|
||||
$(document).on('woocommerce_variations_loaded', toggleLicensedProductVisibility);
|
||||
$(document).on('woocommerce_variations_added', toggleLicensedProductVisibility);
|
||||
$(document).on('woocommerce_variations_saved', toggleLicensedProductVisibility);
|
||||
|
||||
// Handle AJAX complete events for attribute saving
|
||||
$(document).ajaxComplete(function(event, xhr, settings) {
|
||||
// Check if this was a product data save or attribute action
|
||||
if (settings.data && (
|
||||
settings.data.indexOf('action=woocommerce_save_attributes') !== -1 ||
|
||||
settings.data.indexOf('action=woocommerce_load_variations') !== -1 ||
|
||||
settings.data.indexOf('action=woocommerce_add_variation') !== -1
|
||||
)) {
|
||||
setTimeout(toggleLicensedVariableOptions, 100);
|
||||
setTimeout(toggleLicensedProductVisibility, 100);
|
||||
}
|
||||
});
|
||||
|
||||
// Also listen for the WooCommerce product type show/hide trigger
|
||||
$('body').on('woocommerce-product-type-change', toggleLicensedVariableOptions);
|
||||
// Listen for WooCommerce product type show/hide trigger
|
||||
$('body').on('woocommerce-product-type-change', toggleLicensedProductVisibility);
|
||||
});
|
||||
</script>
|
||||
<?php
|
||||
|
||||
Reference in New Issue
Block a user