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

WooCommerce能否按用户自定义变体下拉列表选项?升级许可证需求

实现WooCommerce升级许可证的自定义许可证选择下拉菜单

Hey there! Since you've already got the visibility restriction sorted for your upgrade license (great job on that part!), let's tackle the custom dropdown for users who own multiple copies of your original software. Here's a step-by-step implementation tailored for WooCommerce:

1. 获取用户已购买的初代许可证

首先我们需要一个函数,拉取当前用户名下所有有效的初代软件许可证。这里假设你的初代产品购买后会生成许可证密钥并存储在订单元数据中(如果用第三方许可证管理插件,可替换成对应插件的API调用逻辑)。

function get_user_primary_licenses() {
    $user_id = get_current_user_id();
    if ( !$user_id ) return [];

    // 获取用户所有已完成的订单
    $orders = wc_get_orders([
        'customer_id' => $user_id,
        'status' => 'completed',
        'limit' => -1
    ]);

    $licenses = [];
    foreach ( $orders as $order ) {
        foreach ( $order->get_items() as $item ) {
            // 替换为你的初代产品ID
            $primary_product_id = 123;
            if ( $item->get_product_id() === $primary_product_id ) {
                // 替换'license_key'为你实际存储许可证的元数据键名
                $license_key = $order->get_meta('license_key', true);
                if ( $license_key ) {
                    // 加上订单日期,方便用户区分不同许可证
                    $licenses[$license_key] = sprintf( '许可证 %s (下单日期: %s)', $license_key, $order->get_date_created()->format('Y-m-d') );
                }
            }
        }
    }

    return $licenses;
}

2. 在升级产品页面添加自定义下拉菜单

接下来我们把许可证选择下拉菜单插入到升级许可证产品的「加入购物车」按钮上方:

function add_license_selection_dropdown() {
    // 替换为你的升级许可证产品ID
    $upgrade_product_id = 456;
    if ( get_the_ID() !== $upgrade_product_id ) return;

    $licenses = get_user_primary_licenses();
    if ( empty($licenses) ) return;

    echo '<div class="license-selection" style="margin-bottom: 15px;">';
    echo '<label for="selected_license" style="display: block; margin-bottom: 5px;">选择要升级的许可证:</label>';
    echo '<select id="selected_license" name="selected_license" style="padding: 8px; width: 100%; max-width: 300px;">';
    foreach ( $licenses as $key => $label ) {
        echo sprintf('<option value="%s">%s</option>', esc_attr($key), esc_html($label));
    }
    echo '</select>';
    echo '</div>';
}
add_action('woocommerce_before_add_to_cart_button', 'add_license_selection_dropdown');

3. 验证选择并将许可证信息绑定到购物车项目

我们需要确保用户必须选择许可证才能加入购物车,同时把选中的许可证信息保存到购物车项目中,方便后续订单处理:

// 验证用户是否选择了许可证
function validate_license_selection( $passed, $product_id, $quantity ) {
    $upgrade_product_id = 456;
    if ( $product_id !== $upgrade_product_id ) return $passed;

    if ( empty($_POST['selected_license']) ) {
        wc_add_notice( '请选择要升级的许可证', 'error' );
        $passed = false;
    }
    return $passed;
}
add_action('woocommerce_add_to_cart_validation', 'validate_license_selection', 10, 3);

// 将选中的许可证绑定到购物车项目数据
function add_license_to_cart_item( $cart_item_data, $product_id, $variation_id ) {
    $upgrade_product_id = 456;
    if ( $product_id !== $upgrade_product_id ) return $cart_item_data;

    if ( isset($_POST['selected_license']) ) {
        $license_key = sanitize_text_field($_POST['selected_license']);
        $cart_item_data['selected_license'] = $license_key;
        // 添加唯一标识,确保不同许可证的升级项在购物车中是独立条目
        $cart_item_data['unique_key'] = md5(microtime() . $license_key);
    }
    return $cart_item_data;
}
add_filter('woocommerce_add_cart_item_data', 'add_license_to_cart_item', 10, 3);

4. 在购物车、结账页和订单中显示许可证信息

最后,让用户在购物车、结账页面能看到自己选择的许可证,同时把该信息保存到订单记录里:

// 在购物车项目中显示选中的许可证
function display_license_in_cart( $item_data, $cart_item ) {
    if ( isset($cart_item['selected_license']) ) {
        $item_data[] = [
            'key' => '要升级的许可证',
            'value' => esc_html($cart_item['selected_license'])
        ];
    }
    return $item_data;
}
add_filter('woocommerce_get_item_data', 'display_license_in_cart', 10, 2);

// 将许可证信息保存到订单条目
function save_license_to_order( $item, $cart_item_key, $values, $order ) {
    if ( isset($values['selected_license']) ) {
        $item->add_meta_data('要升级的许可证', $values['selected_license'], true);
    }
}
add_action('woocommerce_checkout_create_order_line_item', 'save_license_to_order', 10, 4);

关键注意事项

  • 替换代码中的$primary_product_id(123)和$upgrade_product_id(456)为你实际的产品ID
  • 如果你使用第三方许可证管理插件(比如WooCommerce License Manager),请将获取许可证的逻辑替换为插件提供的API调用
  • 调整license_key元数据键名,匹配你实际存储初代许可证的字段
  • 可以移除内联CSS,添加自定义类名来适配你的网站样式

内容的提问来源于stack exchange,提问作者Adam Crowe

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 03:54:32