Plugins & Themes

Prevent access to WooCommerce products

5 Replies ·

  1. Hi,

    I’m trying to prevent access to specific products to users in specific roles using the Members “Content Permissions” feature. So as a prerequisite, said product should obviously not be visible to users that are not logged-in.

    But this is not working correctly. The rule seems to be applied but the error message displays as the product’s Long Description and the rest of the product stuff (image, name, price, qty, add to cart button) are all showing above it.

    Is there a way to really hide a product using Members’ “Content Permissions” feature?

    Thanks!

  2. Justin Tadlock

    This actually works as it should. This is because WooCommerce doesn’t output everything as post content (the “content” part of Content Permissions). They only use the long description as content.

    Since WC is a third-party plugin, there’s no way for Members itself to know how to hide a product’s non-content details. Members has a members_can_current_user_view_post() function to allow for customizations like this though.

    What you need to do is edit your WC theme templates. And, where there’s something you want to hide, you can wrap it like so:

    <?php if ( members_can_current_user_view_post() ) : ?>
    
        Only show this content if the user is allowed to see it.
    
    <?php endif; ?>
    

    You’ll likely need to consult the WC documentation on overwriting templates and where to actually put that code.

    I can dig a bit deeper into it tomorrow if you can share with me which theme you’re using.

  3. Stéphane Bergeron

    Hi Justin,

    Thanks for the answer on this. I wasn’t sure that Members only controls the_content but that makes sense.

    I’m using GeneratePress on this project. Any further info you can provide here would be much appreciated. I’d like to avoid installing WooCommerce Memberships just to protect that one product.

    Thank you!

  4. Justin Tadlock

    It looks like your theme doesn’t include any specific WC templates. That’s a good thing because it means we can just filter what WC is outputting.

    I don’t work with WC, so I’m just going with the code I’m seeing. It looks like WC hooks everything in on the single product display. So, what we’ll do is unhook most of that stuff by adding this code to our theme’s functions.php:

    add_action( 'woocommerce_before_single_product', 'th_wc_before_single_product' );
    
    function th_wc_before_single_product() {
    
        if ( function_exists( 'members_can_current_user_view_post' ) && ! members_can_current_user_view_post() ) {
    
            remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_images', 20 );
            remove_action( 'woocommerce_product_thumbnails', 'woocommerce_show_product_thumbnails', 20 );
    
            remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_product_data_tabs', 10 );
            remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_upsell_display', 15 );
            remove_action( 'woocommerce_after_single_product_summary', 'woocommerce_output_related_products', 20 );
    
        //  remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_title', 5 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_rating', 10 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );
        //  remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_meta', 40 );
            remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_sharing', 50 );
        }
    }
    

    You’ll notice I commented out the title and excerpt, so that they’re not removed.

  5. Stéphane Bergeron

    Hello Justin,

    I realize I never thanked you for the code you provided above. It did work for me and all is working as we wanted now.

    Thank you very much!