• Resolved Mat Casner

    (@mcasner)


    I have a couple of products in my WooCommerce Store that I want to only make available to my subscribers. I have created a user level for my subscribers and have assigned them to that user level. I’ve added that level to the WooCommerce Products that I want to restrict, but I’m still able to see/purchase the products, even if I’m not logged in as a user.

    Am I missing something? What am I doing wrong?

    TIA,

    Mat

    • This topic was modified 9 months ago by Mat Casner.
Viewing 4 replies - 1 through 4 (of 4 total)
  • databell96

    (@databell96)

    I’m experiencing something very similar where I assigned user roles to certain products and yet, I can see those even if I’m logged out.

    Plugin Author Caseproof

    (@caseproof)

    Hi @mcasner @databell96

    The Members plugin doesn’t fully protect a product on the frontend. It currently only protects post content. WooCommerce templates show post content (which is protected), but it shows a lot more, such as price, short description, etc., which is usually post meta.
    That being said, here’s a snippet that will redirect logged-in users when they don’t have a required role and the product has sneakers or backpacks categories:

    add_filter( 'woocommerce_product_is_visible', 'members_maybe_remove_product_from_query', 95, 2 );
    /**
     * Removes a product from the query depending on if the user has the correct permission.
     *
     * @param boolean 	$is_visible 	Whether the product is visible
     * @param int 		$product_id 	Product ID
     *
     * @return boolean
     */
    function members_maybe_remove_product_from_query( $is_visible, $product_id ) {
    	return members_can_current_user_view_post( $product_id );
    }
    add_action( 'template_redirect', 'members_woocommerce_product_redirect' );
    function members_woocommerce_product_redirect() {
      if ( class_exists( 'WooCommerce' ) && is_singular( 'product' ) ) {
        $user = wp_get_current_user();
        $post_id = get_the_ID();
        if( ! empty($user) && in_array( 'author', (array) $user->roles ) && has_term( array( 'sneakers', 'backpacks' ), 'product_cat', $post_id ) ) {
          wp_redirect( apply_filters( 'members_woocommerce_product_redirect', get_permalink( wc_get_page_id( 'shop' ) ) ) );
          exit;
        }
      }
    }

    If you want to use it for particular product, you’ll need to modify has_term part in the condition above.

    I hope that helps.

    Thread Starter Mat Casner

    (@mcasner)

    Thank you @caseproof… Where in the snippet do you define the Members’ role?

    Plugin Author Caseproof

    (@caseproof)

    In this line:

    if( ! empty($user) && in_array( 'author', (array) $user->roles ) && has_term( array( 'sneakers', 'backpacks' ), 'product_cat', $post_id ) ) {

    there is an author role so you will need to replace it.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Restrict WooCommerce Products’ is closed to new replies.