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

@@ -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 = [];
}