基于LearnDash+WooCommerce的LMS开发:隐藏已购商品并阻止重复购买的技术求助
Hey Ramon, great job getting the duplicate purchase prevention working already! Let’s tackle that second requirement—hiding purchased products from the WooCommerce shop page. Here’s how you can do it step by step:
1. Your Existing Duplicate Purchase Prevention Code (Verified)
First, let’s confirm your current code works perfectly to stop logged-in users from re-adding purchased products to their cart. Here’s the code again for reference, with a small clarifying note:
/* Avoid buying the same product again in WooCommerce by adding products to the cart by logged-in users before checkout */ function filter_woocommerce_add_to_cart_validation( $passed, $product_id, $quantity, $variation_id = null, $variations = null) { // Retrieve the current user object $current_user = wp_get_current_user(); // Use variation ID if available (handles variable products correctly) $product_id = $variation_id > 0 ? $variation_id : $product_id; // Check if user has already bought this product (including variations) if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product_id ) ) { // Display error message wc_add_notice( __( 'You have already enrolled into the course.', 'woocommerce' ), 'error' ); $passed = false; } return $passed; } add_filter( 'woocommerce_add_to_cart_validation', 'filter_woocommerce_add_to_cart_validation', 10, 5 );
This code correctly blocks duplicate purchases by checking if a logged-in user has already bought the product (or its variation) and shows a clear error message. Nice work here!
2. Hide Purchased Products from Shop/Product List Pages
To hide products the user has already purchased from the WooCommerce shop grid, we’ll use the woocommerce_product_query filter. This lets us modify the product database query before it runs, excluding any items the user has bought.
Add this code to your theme’s functions.php file, or a custom plugin (recommended for long-term maintainability):
/* Hide purchased products from WooCommerce shop and product list pages */ function hide_purchased_products_from_shop( $q ) { // Only run for logged-in users on shop/archive pages if ( is_user_logged_in() && ( is_shop() || is_product_category() || is_product_tag() ) ) { $current_user = wp_get_current_user(); // Get all product IDs the user has purchased $purchased_product_ids = wc_get_customer_order_ids( $current_user->ID ); $purchased_ids = array(); // Loop through user's orders to extract product/variation IDs foreach ( $purchased_product_ids as $order_id ) { $order = wc_get_order( $order_id ); if ( $order ) { foreach ( $order->get_items() as $item ) { $product_id = $item->get_product_id(); $variation_id = $item->get_variation_id(); // Add both parent product and variation IDs to exclusion list $purchased_ids[] = $product_id; if ( $variation_id > 0 ) { $purchased_ids[] = $variation_id; } } } } // Remove duplicate IDs (in case user bought the same product multiple times) $purchased_ids = array_unique( $purchased_ids ); // Exclude purchased products from the query if ( ! empty( $purchased_ids ) ) { $q->set( 'post__not_in', $purchased_ids ); } } } add_action( 'woocommerce_product_query', 'hide_purchased_products_from_shop' );
How This Works:
- We first check if the user is logged in and viewing a shop-related page (main shop, product category, or product tag) to avoid affecting other parts of your site.
- We fetch all the user’s past orders, then extract every product and variation ID they’ve purchased.
- We clean up duplicate IDs to avoid redundant exclusions.
- Finally, we modify the product query to exclude these purchased IDs, so they won’t appear in the shop grid.
3. Testing the Solution
- Log in as a user who has purchased some products.
- Visit your WooCommerce shop page—you’ll only see products you haven’t bought yet (matching your example where Items B and D are hidden).
- Try adding a purchased product to your cart—you’ll still get the error message from your original code, reinforcing the duplicate purchase block.
This combination fully addresses both of your core requirements for your LearnDash + WooCommerce LMS.
内容的提问来源于stack exchange,提问作者Ramon

