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 9c4232f14f
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] ## [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 ## [0.5.9] - 2026-01-27
### Fixed ### Fixed

View File

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