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

EDD多下载项用户重复购买限制功能开发问题

Fixing EDD Purchase Limitation for Multiple Cart Items

Got it, your current code works great for single product IDs, but it doesn’t account for when customers have multiple downloads in their cart. Let’s tweak it to handle both scenarios seamlessly.

Updated Code

// Restrict checkout if user has already purchased any item in their cart
add_filter( 'edd_can_checkout', 'sd_limit_purchase' );
function sd_limit_purchase( $can_checkout ) {
    // Bypass if user isn't logged in (remove this if you want to restrict guests too)
    if ( ! is_user_logged_in() ) {
        return $can_checkout;
    }

    $user_id = get_current_user_id();
    $cart_contents = edd_get_cart_contents();

    // Loop through each item in the cart to check purchase history
    foreach ( $cart_contents as $cart_item ) {
        $download_id = $cart_item['id'];
        
        // Use EDD's native function to check if user owns this download
        if ( edd_has_user_purchased( $user_id, $download_id ) ) {
            // Set a clear error message for the user
            edd_set_error( 'duplicate_purchase', __( 'You’ve already purchased one or more items in your cart. Please remove them to continue checkout.', 'your-text-domain' ) );
            $can_checkout = false;
            // Stop checking once we find a purchased item
            break;
        }
    }

    return $can_checkout;
}

What Changed & Why

  • Cart Iteration: Instead of targeting a single product, we loop through every item in the cart with edd_get_cart_contents()—this covers multi-item scenarios.
  • Native EDD Function: edd_has_user_purchased() is EDD’s official method for checking user purchase history, so it’s more reliable than manual purchase queries.
  • Clear Error Feedback: We use edd_set_error() to display a user-friendly message explaining why checkout is blocked.
  • Efficient Check: Once we find one already-purchased item, we break the loop to avoid unnecessary processing.

Optional Tweaks

  • Guest Restrictions: If you want to block guests from repurchasing, remove the is_user_logged_in() check and verify purchases using the customer’s email (use edd_get_purchases( array( 'email' => edd_get_cart_email(), 'download_id' => $download_id ) ) for this).
  • Exclude Specific Products: Add a condition like if ( ! in_array( $download_id, array( 123, 456 ) ) ) before checking purchase history to allow multiple buys for certain downloads.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 12:03:01