• Resolved janmagn

    (@janmagn)


    Hi, I want to assign a user role (wholesale customers) to be able to view a product category. While all other users (normal clients) will not be able to view it. Is this achievable using this plugin? Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author Caseproof

    (@caseproof)

    Hi @janmagn

    The Members plugin doesn’t fully protect a product on the frontend. It currently only protects post content. Assuming that you’re using WooCommerce, keep in mind that WooCommerce templates show post content (which will be protected), but it shows a lot more, such as price, short description, etc., which is usually post meta.
    That’s being said, here’s a snippet that will redirect logged-in users when the user doesn’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;
        }
      }
    }

    Hopefully, that helps.

    Thread Starter janmagn

    (@janmagn)

    Thanks a lot!

    Hi. Please can you advise where this snippet needs to be added? Thanks, Tasha

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to restrict a product category to be visible to a single user role’ is closed to new replies.