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

@@ -338,19 +338,45 @@ final class LicensedProductType
{
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;
}
// 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(
'single-product/add-to-cart/variable.php',
[
'available_variations' => $getVariations ? $product->get_available_variations() : false,
'attributes' => $product->get_variation_attributes(),
'selected_attributes' => $product->get_default_attributes(),
'available_variations' => $availableVariations,
'attributes' => $attributes,
'selected_attributes' => $selectedAttributes,
]
);
}