From 9c4232f14fa7535469f3cec85b074b01a7439a26 Mon Sep 17 00:00:00 2001 From: magdev Date: Tue, 27 Jan 2026 13:51:46 +0100 Subject: [PATCH] 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 --- CHANGELOG.md | 9 +++++++ src/Product/LicensedProductType.php | 39 +++++++++++++++-------------- wc-licensed-product.php | 4 +-- 3 files changed, 31 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 983b4bb..6902854 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.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 diff --git a/src/Product/LicensedProductType.php b/src/Product/LicensedProductType.php index cc57075..7abb461 100644 --- a/src/Product/LicensedProductType.php +++ b/src/Product/LicensedProductType.php @@ -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 '

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

'; + 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 = []; } diff --git a/wc-licensed-product.php b/wc-licensed-product.php index f49f01d..bb4d435 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.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__));