Stan_Joco
Forum Replies Created
-
Forum: Plugins
In reply to: Woocommerce Restrict or Hide Products and Categories from specific user rolesHuge thanks to SideKick Dan for posting the original code!!!
Here is a copy of the final code I used.
Changes I made to the original code:
1. Changed the product category slug from ‘specials’ to ‘display’
2. Changed one of the users that could view the hidden prod. category from
‘customer’ to ‘wholesale_customer’
3. Removed ‘&& is_shop()’ from the if statement (this appears twice in the original code). The original code left the category visible in the woocommerce widget.My revised Code.
// Woocommerce - Redirect unauthorised users from accessing a specified product category when clicked or visited via direct url function woocommerce_hide_non_registered() { if( ( is_product_category('display') ) && ! ( current_user_can( 'wholesale_customer' ) || current_user_can( 'administrator' ) ) ) { wp_redirect( site_url( '/' )); exit(); } } add_action( 'template_redirect','woocommerce_hide_non_registered' ); // End - Woocommerce - redirect unauthorised users from accessing a specified product category // Woocommerce - Removes category link from woocommerce product category widgets so they are not seen add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 ); function get_subcategory_terms( $terms, $taxonomies, $args ) { $new_terms = array(); // if a product category and on the shop page if ( in_array( 'product_cat', $taxonomies ) && ! ( current_user_can( 'wholesale_customer' ) || current_user_can( 'administrator' ) ) ) { foreach ( $terms as $key => $term ) { if ( ! in_array( $term->slug, array( 'display' ) ) ) { $new_terms[] = $term; } } $terms = $new_terms; } return $terms; } // End - Woocommerce - Removes category link from woocommerce product category widgets so they are not seen // Woocommerce - Remove products from being displayed that belong to a category user is not authorised to visit. Products seem to still be accessible via direct url unfortunately. add_action( 'pre_get_posts', 'custom_pre_get_posts' ); function custom_pre_get_posts( $q ) { if ( ! $q->is_main_query() ) return; if ( ! $q->is_post_type_archive() ) return; if ( ! ( current_user_can( 'wholesale_customer' ) || current_user_can( 'administrator' ) ) ) { $q->set( 'tax_query', array(array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => array( 'display'), // Don't display products in the private-clients category on the shop page 'operator' => 'NOT IN' ))); } remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' ); } // End - Woocommerce - Remove products from being displayed that belong to a category user is not authorised to visit. Products seem to still be accessible via direct url unfortunately.
Forum: Plugins
In reply to: Woocommerce Restrict or Hide Products and Categories from specific user roles@jgoug:
I think it will work correctly if you assigned the few users into a specific user role such as “subscriber” or “author”You might have to customize the user privileges to match your needs.
Forum: Plugins
In reply to: Woocommerce Restrict or Hide Products and Categories from specific user rolesHey Dan,
Thank you for posting this. This is exactly what I need but for some reason it does not seem to be hiding the category from the woocommerce product category widget. Any ideas?
-Category i’m trying to hide ‘display’
-I would like the Category ‘display’ to be visable only to ‘wholesale customer’ and ‘administrator’-website link here
Here is the code I have.
// Woocommerce - Redirect unauthorised users from accessing a specified product category when clicked or visited via direct url function woocommerce_hide_non_registered() { if( ( is_product_category('display') ) && ! ( current_user_can( 'wholesale customer' ) || current_user_can( 'administrator' ) ) ) { wp_redirect( site_url( '/' )); exit(); } } add_action( 'template_redirect','woocommerce_hide_non_registered' ); // End - Woocommerce - redirect unauthorised users from accessing a specified product category // Woocommerce - Removes category link from woocommerce product category widgets so they are not seen add_filter( 'get_terms', 'get_subcategory_terms', 10, 3 ); function get_subcategory_terms( $terms, $taxonomies, $args ) { $new_terms = array(); // if a product category and on the shop page if ( in_array( 'product_cat', $taxonomies ) && ! ( current_user_can( 'wholesale customer' ) || current_user_can( 'administrator' ) ) && is_shop() ) { foreach ( $terms as $key => $term ) { if ( ! in_array( $term->slug, array( 'display' ) ) ) { $new_terms[] = $term; } } $terms = $new_terms; } return $terms; } // End - Woocommerce - Removes category link from woocommerce product category widgets so they are not seen // Woocommerce - Remove products from being displayed that belong to a category user is not authorised to visit. Products seem to still be accessible via direct url unfortunately. add_action( 'pre_get_posts', 'custom_pre_get_posts' ); function custom_pre_get_posts( $q ) { if ( ! $q->is_main_query() ) return; if ( ! $q->is_post_type_archive() ) return; if ( ! ( current_user_can( 'wholesale customer' ) || current_user_can( 'administrator' ) ) && is_shop() ) { $q->set( 'tax_query', array(array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => array( 'display'), // Don't display products in the private-clients category on the shop page 'operator' => 'NOT IN' ))); } remove_action( 'pre_get_posts', 'custom_pre_get_posts_query' ); } // End - Woocommerce - Remove products from being displayed that belong to a category user is not authorised to visit. Products seem to still be accessible via direct url unfortunately.