Fix frontend error on licensed variable products without attributes (v0.5.9)

- Added null checks for get_variation_attributes(), get_available_variations(), get_default_attributes()
- Show informative message when product has no variations configured
- Changed product type check from instanceof to is_type() for better compatibility

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-27 13:40:50 +01:00
parent fa972ceaf0
commit 9826c8181e
3 changed files with 42 additions and 7 deletions

View File

@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] ## [Unreleased]
## [0.5.9] - 2026-01-27
### Fixed
- Fixed frontend error on licensed variable products when no attributes are defined
- Added null checks for `get_variation_attributes()`, `get_available_variations()`, and `get_default_attributes()`
- Show informative message instead of error when product has no variations configured
- Changed product type check from `instanceof` to `is_type()` for better compatibility
## [0.5.8] - 2026-01-27 ## [0.5.8] - 2026-01-27
### Fixed ### Fixed

View File

@@ -338,19 +338,45 @@ final class LicensedProductType
{ {
global $product; global $product;
if (!$product instanceof \WC_Product_Variable) { // Check if product is a variable type (includes LicensedVariableProduct which extends WC_Product_Variable)
if (!$product || !$product->is_type('licensed-variable')) {
return;
}
// Get attributes - ensure we have an array even if no attributes are set
$attributes = $product->get_variation_attributes();
if (!is_array($attributes)) {
$attributes = [];
}
// If no attributes defined, show a message instead of broken form
if (empty($attributes)) {
echo '<p class="woocommerce-info">' . esc_html__('This product has no variations available.', 'wc-licensed-product') . '</p>';
return; return;
} }
// Get variations count to determine if we should load them via AJAX // Get variations count to determine if we should load them via AJAX
$getVariations = count($product->get_children()) <= apply_filters('woocommerce_ajax_variation_threshold', 30, $product); $children = $product->get_children();
$getVariations = count($children) <= apply_filters('woocommerce_ajax_variation_threshold', 30, $product);
// Get available variations - ensure we have an array
$availableVariations = $getVariations ? $product->get_available_variations() : false;
if ($getVariations && !is_array($availableVariations)) {
$availableVariations = [];
}
// Get default/selected attributes - ensure we have an array
$selectedAttributes = $product->get_default_attributes();
if (!is_array($selectedAttributes)) {
$selectedAttributes = [];
}
wc_get_template( wc_get_template(
'single-product/add-to-cart/variable.php', 'single-product/add-to-cart/variable.php',
[ [
'available_variations' => $getVariations ? $product->get_available_variations() : false, 'available_variations' => $availableVariations,
'attributes' => $product->get_variation_attributes(), 'attributes' => $attributes,
'selected_attributes' => $product->get_default_attributes(), 'selected_attributes' => $selectedAttributes,
] ]
); );
} }

View File

@@ -3,7 +3,7 @@
* Plugin Name: WooCommerce Licensed Product * Plugin Name: WooCommerce Licensed Product
* Plugin URI: https://src.bundespruefstelle.ch/magdev/wc-licensed-product * Plugin URI: https://src.bundespruefstelle.ch/magdev/wc-licensed-product
* Description: WooCommerce plugin to sell software products using license keys with domain-based validation. * Description: WooCommerce plugin to sell software products using license keys with domain-based validation.
* Version: 0.5.8 * Version: 0.5.9
* Author: Marco Graetsch * Author: Marco Graetsch
* Author URI: https://src.bundespruefstelle.ch/magdev * Author URI: https://src.bundespruefstelle.ch/magdev
* License: GPL-2.0-or-later * License: GPL-2.0-or-later
@@ -28,7 +28,7 @@ if (!defined('ABSPATH')) {
} }
// Plugin constants // Plugin constants
define('WC_LICENSED_PRODUCT_VERSION', '0.5.8'); define('WC_LICENSED_PRODUCT_VERSION', '0.5.9');
define('WC_LICENSED_PRODUCT_PLUGIN_FILE', __FILE__); define('WC_LICENSED_PRODUCT_PLUGIN_FILE', __FILE__);
define('WC_LICENSED_PRODUCT_PLUGIN_DIR', plugin_dir_path(__FILE__)); define('WC_LICENSED_PRODUCT_PLUGIN_DIR', plugin_dir_path(__FILE__));
define('WC_LICENSED_PRODUCT_PLUGIN_URL', plugin_dir_url(__FILE__)); define('WC_LICENSED_PRODUCT_PLUGIN_URL', plugin_dir_url(__FILE__));