skip to Main Content

I am creating an LMS using LearnDash + WooCommerce. So far so good. Now, I’d like to hide a Product when the logged-in user has already purchased it to a) prevent the user from purchasing it twice, and b) so that I can display a product grid for them without the already purchased item(s).

Another example: if a user who purchased ITEM A goes to the shop, ITEM A should not even be displayed for them.

Thanks so much!

3

Answers


  1. Chosen as BEST ANSWER

    So, I ended up with a nice workaround:

    On the product page, I created a CUSTOM FIELD where I can input the COURSE ID.

    Then, with PHP, I check the Product and retrieve the custom field for the COURSE ID.

    LearnDash has a shortcode for students... so after retrieving the course ID, I can display a message/button for users who have registered already for this course.

    This is the code:

    <?php
        global $product;
        $postid = $product->get_id();
        $courseid = get_post_meta( $product->get_id(), 'ID_del_curso', true );
        $html = '"]<center><div class="alreadyPurchased"><i class="fas fa-graduation-cap paddingRightM"></i> Ya te has registrado</div></center>';
        $openIT = '[student course_id="';
        $closeIT = '[/student]';
        echo do_shortcode( $openIT . $courseid . $html . $closeIT ); 
    ?>
    

    Hope it helps!


  2. /**
     * Disables repeat purchase for products / variations
     * 
     * @param bool $purchasable true if product can be purchased
     * @param WC_Product $product the WooCommerce product
     * @return bool $purchasable the updated is_purchasable check
     */
    function sv_disable_repeat_purchase( $purchasable, $product ) {
    
        // Don't run on parents of variations,
        // function will already check variations separately
        if ( $product->is_type( 'variable' ) ) {
            return $purchasable;
        }
    
        // Get the ID for the current product (passed in)
        $product_id = $product->get_id(); 
    
        // return false if the customer has bought the product / variation
        if ( wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $product_id ) ) {
            $purchasable = false;
        }
    
        return $purchasable;
    }
    add_filter( 'woocommerce_is_purchasable', 'sv_disable_repeat_purchase', 10, 2 );
    
    
    /**
     * Shows a "purchase disabled" message to the customer
     */
    function sv_purchase_disabled_message() {
        global $product; // get the current product to see if it has been purchased
    
        if ( $product->is_type( 'variable' ) ) {
    
            foreach ( $product->get_children() as $variation_id ) {
                // Render the purchase restricted message if it has been purchased
                if ( wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $variation_id ) ) {
                    sv_render_variation_non_purchasable_message( $product, $variation_id );
                }
            }
    
        } else {
    
            if ( wc_customer_bought_product( wp_get_current_user()->user_email, get_current_user_id(), $product->get_id() ) ) {
                echo '<div class="woocommerce"><div class="woocommerce-info wc-nonpurchasable-message">You've already purchased this product! It can only be purchased once.</div></div>';
            }
        }
    }
    add_action( 'woocommerce_single_product_summary', 'sv_purchase_disabled_message', 31 );
    
    
    /**
     * Generates a "purchase disabled" message to the customer for specific variations
     * 
     * @param WC_Product $product the WooCommerce product
     * @param int $no_repeats_id the id of the non-purchasable product
     */
    function sv_render_variation_non_purchasable_message( $product, $no_repeats_id ) {
    
        // Double-check we're looking at a variable product
        if ( $product->is_type( 'variable' ) && $product->has_child() ) {
    
            $variation_purchasable = true;
    
            foreach ( $product->get_available_variations() as $variation ) {
    
                // only show this message for non-purchasable variations matching our ID
                if ( $no_repeats_id === $variation['variation_id'] ) {
                    $variation_purchasable = false; 
                    echo '<div class="woocommerce"><div class="woocommerce-info wc-nonpurchasable-message js-variation-' . sanitize_html_class( $variation['variation_id'] ) . '">You've already purchased this product! It can only be purchased once.</div></div>';
                }
            }
        }
    
        if ( ! $variation_purchasable ) {
            wc_enqueue_js("
                jQuery('.variations_form')
                    .on( 'woocommerce_variation_select_change', function( event ) {
                        jQuery('.wc-nonpurchasable-message').hide();
                    })
                    .on( 'found_variation', function( event, variation ) {
                        jQuery('.wc-nonpurchasable-message').hide();
                        if ( ! variation.is_purchasable ) {
                            jQuery( '.wc-nonpurchasable-message.js-variation-' + variation.variation_id ).show();
                        }
                    })
                .find( '.variations select' ).change();
            ");
        }
    }
    

    Gist snippet got from here – please check

    Login or Signup to reply.
  3. There is a simpler solution.
    Use this LearnDash shortcode…

    [visitor]
    Hello visitor.
    Put your "Buy now" button here.
    [/visitor]

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search