You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WooCommerce自定义可变礼品卡产品缺货及变体不显示问题

Fix: Custom Variable Product Type (Gift Card) Not Showing Variations in WooCommerce Frontend

I've run into this exact issue before when building custom variable product types in WooCommerce—here's what's going wrong and how to fix it:

The Root Problem

WooCommerce doesn't automatically treat subclasses of WC_Product_Variable as variable products. Most of its core logic for loading attributes, variations, and checking stock explicitly checks if the product type equals variable, not if it inherits from the variable product class. That's why your backend works (you manually enabled the tabs) but the frontend can't load variations or recognize available stock.

Step-by-Step Fixes

1. Tell WooCommerce Your Custom Type Is a Variable Product

Add these filters to mark your gift card type as a variable product and ensure variants are queried correctly:

// Mark gift card as a variable product type
add_filter( 'woocommerce_is_variable_product', 'knl_shop_giftcard_is_variable_product', 10, 2 );
function knl_shop_giftcard_is_variable_product( $is_variable, $product ) {
    if ( is_a( $product, 'KNL_SHOP_WC_GIFTCARD_PROD_TYPE' ) ) {
        return true;
    }
    return $is_variable;
}

// Ensure variations are fetched for gift card parent products
add_filter( 'woocommerce_product_variation_query_args', 'knl_shop_giftcard_variation_query_args' );
function knl_shop_giftcard_variation_query_args( $args ) {
    if ( isset( $args['post_parent'] ) ) {
        $parent_product = wc_get_product( $args['post_parent'] );
        if ( $parent_product && 'knl_shop_giftcard' === $parent_product->get_type() ) {
            $args['meta_query'][] = array(
                'key'     => '_parent_product_type',
                'value'   => 'knl_shop_giftcard',
                'compare' => '=',
            );
        }
    }
    return $args;
}

2. Fix Stock Status & Variation Loading in Your Custom Class

Update your KNL_SHOP_WC_GIFTCARD_PROD_TYPE class to override stock checks (since variable products rely on variant stock, not parent stock) and ensure variations load properly:

class KNL_SHOP_WC_GIFTCARD_PROD_TYPE extends WC_Product_Variable {
    public function __construct( $product = 0 ) {
        parent::__construct( $product );
    }

    public function get_type() {
        return 'knl_shop_giftcard';
    }

    // Override stock check to look at variant stock
    public function is_in_stock() {
        foreach ( $this->get_available_variations() as $variation_data ) {
            $variation = wc_get_product( $variation_data['variation_id'] );
            if ( $variation && $variation->is_in_stock() ) {
                return true;
            }
        }
        return false;
    }

    // Ensure correct stock status is returned
    public function get_stock_status() {
        return $this->is_in_stock() ? 'instock' : 'outofstock';
    }
}

3. Verify Your giftcard.php Template

Double-check that your copied template is pulling data directly from the product object (not relying on hardcoded variable type checks). At the top of giftcard.php, make sure you have:

global $product;
$attributes = $product->get_variation_attributes();
$available_variations = $product->get_available_variations();

Final Testing Steps

  1. Go to WooCommerce > Status > Tools and clear all transients/caches
  2. Re-save your gift card product (this refreshes product metadata)
  3. Refresh the frontend product page—your variations should now load, and the out-of-stock message should disappear if any variant is in stock

内容的提问来源于stack exchange,提问作者Assad Nazar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 13:07:42