• Resolved InventiveWebDesign

    (@inventivewebdesign)


    I am adding a parts store to my Members site. I’d like to be able to restrict access to either products(parts) or categories using WooCommerce.

    Our users are assigned roles based on the product(s) they purchase from us. We would like them to only be able to see the parts that pertain to their product.

    The easiest way would be to be able to assign a category for each of our products to each part (i.e. Part 1 has Category Product A). Then we can just say that Users with the role of Product A can only see any part with the category and not any of the other parts. If not possible we can restrict access to each part by user role.

    If I go to a product(part) in WooCommerce and scroll down I can see the content restriction section but if I restrict a product to a user role I can still see it on the Shop page and the individual product page. It doesn’t look like it restricts it at all!

    Look here on my dev site: https://inventivewebdesign.com/dev/shop/

    I have Assorted Coffee and Cashew Butter set to be restricted to User role Product 1 and Product 3. Even if I am not logged in I can see them.

    Any suggestions would be great!

Viewing 1 replies (of 1 total)
  • Plugin Author Caseproof

    (@caseproof)

    Hi @inventivewebdesign

    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:

    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.

Viewing 1 replies (of 1 total)
  • The topic ‘Restrict access to WooCommerce Category or Product’ is closed to new replies.