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

WooCommerce如何实现单个客户终身仅可购买指定产品一次?

完整可直接使用的整合代码

你可以直接用下面的代码替换你现有的两段代码,既保留原有的自动免邮功能,也解决所有限购绕过的问题:

// 定义全局配置:限购产品ID、免邮优惠券码、限购提示文案
define('LIMIT_PRODUCT_ID', 7946);
define('FREE_SHIP_COUPON', 'GRATISverzendingProefbox');
define('LIMIT_NOTICE', 'Je hebt onze proefbox al eens gekocht. Door de grote vraag is het helaas niet mogelijk om het nog een keer te kopen.');

// 1. 加购校验
add_filter( 'woocommerce_add_to_cart_validation', 'custom_limit_product_purchase', 10, 5 );
function custom_limit_product_purchase( $passed, $product_id, $quantity, $variation_id = null, $variations = null ) {
    // 仅校验限购产品
    if ($product_id != LIMIT_PRODUCT_ID) return $passed;
    
    $current_user = wp_get_current_user();
    // 已登录用户直接校验,未登录用户暂时放过,后续结账/登录时再校验
    if ( $current_user->ID > 0 && wc_customer_bought_product( $current_user->user_email, $current_user->ID, LIMIT_PRODUCT_ID ) ) {
        wc_add_notice( __( LIMIT_NOTICE, 'woocommerce' ), 'error' );
        $passed = false;
    }
    return $passed;
}

// 2. 购物车页/结账页加载时校验
add_action( 'woocommerce_check_cart_items', 'custom_check_cart_limit' );
add_action( 'woocommerce_before_checkout_form', 'custom_check_cart_limit' );
function custom_check_cart_limit() {
    $current_user = wp_get_current_user();
    $has_limit_product = false;
    // 检查购物车是否有限购产品
    foreach( WC()->cart->get_cart() as $cart_item ) {
        if ( $cart_item['product_id'] == LIMIT_PRODUCT_ID ) {
            $has_limit_product = true;
            break;
        }
    }
    if (!$has_limit_product) return;
    
    // 已登录用户校验
    if ( $current_user->ID > 0 && wc_customer_bought_product( $current_user->user_email, $current_user->ID, LIMIT_PRODUCT_ID ) ) {
        wc_clear_notices();
        wc_add_notice( __( LIMIT_NOTICE, 'woocommerce' ), 'error' );
        remove_action( 'woocommerce_proceed_to_checkout', 'woocommerce_button_proceed_to_checkout', 20 );
        // 自动移除限购产品
        foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
            if ( $cart_item['product_id'] == LIMIT_PRODUCT_ID ) {
                WC()->cart->remove_cart_item( $cart_item_key );
            }
        }
    }
}

// 3. 用户登录后立刻校验购物车
add_action('wp_login', 'custom_check_limit_after_login', 10, 2);
function custom_check_limit_after_login($user_login, $user) {
    if ( !WC()->cart ) return;
    foreach( WC()->cart->get_cart() as $cart_item ) {
        if ( $cart_item['product_id'] == LIMIT_PRODUCT_ID && wc_customer_bought_product( $user->user_email, $user->ID, LIMIT_PRODUCT_ID ) ) {
            wc_add_notice( __( LIMIT_NOTICE, 'woocommerce' ), 'error' );
            // 移除限购产品
            foreach( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
                if ( $cart_item['product_id'] == LIMIT_PRODUCT_ID ) {
                    WC()->cart->remove_cart_item( $cart_item_key );
                }
            }
            break;
        }
    }
}

// 4. 结账提交前最终校验(覆盖未登录访客场景)
add_action('woocommerce_checkout_process', 'custom_check_limit_before_checkout');
function custom_check_limit_before_checkout() {
    $billing_email = sanitize_email($_POST['billing_email']);
    foreach( WC()->cart->get_cart() as $cart_item ) {
        if ( $cart_item['product_id'] == LIMIT_PRODUCT_ID && wc_customer_bought_product( $billing_email, 0, LIMIT_PRODUCT_ID ) ) {
            wc_add_notice( __( LIMIT_NOTICE, 'woocommerce' ), 'error' );
            break;
        }
    }
}

// 原有自动免邮功能保留
add_action( 'woocommerce_before_cart', 'bbloomer_apply_matched_coupons' );
function bbloomer_apply_matched_coupons() {
    $coupon_code = FREE_SHIP_COUPON; 
    if ( WC()->cart->has_discount( $coupon_code ) ) return;
    foreach ( WC()->cart->get_cart() as $cart_item_key => $cart_item ) {
        if ( $cart_item['product_id'] == LIMIT_PRODUCT_ID ) {   
            WC()->cart->apply_coupon( $coupon_code );
            wc_print_notices();
        }
    }
}

功能说明

  • 全场景覆盖限购规则:登录用户加购校验、访客结账邮箱校验、用户加购后登录的自动校验、结账提交前最终校验,无绕过漏洞
  • 保留原有自动应用免邮优惠券的逻辑,无需额外配置
  • 所有可修改的参数都统一放在代码开头的定义里,后续要更换产品ID、优惠券码、提示文案直接修改对应值即可

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.07 01:33:02