Fix licensed variable products not showing variations (v0.5.10)

- Re-load product via wc_get_product() to ensure correct class instance
- Removed overly strict type check that prevented variations from displaying
- Now mirrors WooCommerce's standard woocommerce_variable_add_to_cart()

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
2026-01-27 13:51:46 +01:00
parent 0638767ce3
commit 4440fb2e65
3 changed files with 31 additions and 21 deletions

View File

@@ -7,6 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
## [0.5.10] - 2026-01-27
### Fixed
- Fixed licensed variable products not showing variations even when attributes are defined
- Re-load product via `wc_get_product()` to ensure correct class instance is used
- Removed overly strict type check that was preventing variations from displaying
- Now mirrors WooCommerce's standard `woocommerce_variable_add_to_cart()` implementation
## [0.5.9] - 2026-01-27
### Fixed

View File

@@ -333,40 +333,41 @@ final class LicensedProductType
/**
* Add to cart template for variable licensed products
* This mirrors WooCommerce's woocommerce_variable_add_to_cart() function
*/
public function variableAddToCartTemplate(): void
{
global $product;
// Check if product is a variable type (includes LicensedVariableProduct which extends WC_Product_Variable)
if (!$product || !$product->is_type('licensed-variable')) {
// The hook woocommerce_licensed-variable_add_to_cart only fires for this product type
// so we just need to verify the product exists
if (!$product) {
return;
}
// Get attributes - ensure we have an array even if no attributes are set
$attributes = $product->get_variation_attributes();
if (!is_array($attributes)) {
$attributes = [];
}
// Ensure we're working with a product that has variable product methods
// Re-load the product to ensure we get the correct class instance
$productId = $product->get_id();
$variableProduct = wc_get_product($productId);
// 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>';
if (!$variableProduct || !method_exists($variableProduct, 'get_variation_attributes')) {
// Fallback to simple add to cart if not a variable product
wc_get_template('single-product/add-to-cart/simple.php');
return;
}
// Get variations count to determine if we should load them via AJAX
$children = $product->get_children();
$getVariations = count($children) <= apply_filters('woocommerce_ajax_variation_threshold', 30, $product);
$getVariations = count($variableProduct->get_children()) <= apply_filters('woocommerce_ajax_variation_threshold', 30, $variableProduct);
// Get available variations - ensure we have an array
$availableVariations = $getVariations ? $product->get_available_variations() : false;
if ($getVariations && !is_array($availableVariations)) {
$availableVariations = [];
// Get template variables - WooCommerce expects these to be set
$availableVariations = $getVariations ? $variableProduct->get_available_variations() : false;
$attributes = $variableProduct->get_variation_attributes();
$selectedAttributes = $variableProduct->get_default_attributes();
// Ensure arrays (WooCommerce template expects arrays, not null)
if (!is_array($attributes)) {
$attributes = [];
}
// Get default/selected attributes - ensure we have an array
$selectedAttributes = $product->get_default_attributes();
if (!is_array($selectedAttributes)) {
$selectedAttributes = [];
}

View File

@@ -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.9
* Version: 0.5.10
* 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.9');
define('WC_LICENSED_PRODUCT_VERSION', '0.5.10');
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__));