From 9826c8181ef6663078417b29a86496c8b212202a Mon Sep 17 00:00:00 2001 From: magdev Date: Tue, 27 Jan 2026 13:40:50 +0100 Subject: [PATCH] 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 --- CHANGELOG.md | 9 ++++++++ src/Product/LicensedProductType.php | 36 +++++++++++++++++++++++++---- wc-licensed-product.php | 4 ++-- 3 files changed, 42 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 915ef7e..983b4bb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [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 ### Fixed diff --git a/src/Product/LicensedProductType.php b/src/Product/LicensedProductType.php index 124c91c..cc57075 100644 --- a/src/Product/LicensedProductType.php +++ b/src/Product/LicensedProductType.php @@ -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 '

' . esc_html__('This product has no variations available.', 'wc-licensed-product') . '

'; 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, ] ); } diff --git a/wc-licensed-product.php b/wc-licensed-product.php index 722cf66..f49f01d 100644 --- a/wc-licensed-product.php +++ b/wc-licensed-product.php @@ -3,7 +3,7 @@ * Plugin Name: WooCommerce 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. - * Version: 0.5.8 + * Version: 0.5.9 * Author: Marco Graetsch * Author URI: https://src.bundespruefstelle.ch/magdev * License: GPL-2.0-or-later @@ -28,7 +28,7 @@ if (!defined('ABSPATH')) { } // 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_DIR', plugin_dir_path(__FILE__)); define('WC_LICENSED_PRODUCT_PLUGIN_URL', plugin_dir_url(__FILE__));