Skip to content

Instantly share code, notes, and snippets.

@TanvirHasan19
Created December 16, 2025 06:59
Show Gist options
  • Select an option

  • Save TanvirHasan19/455f0422d0ecccaf0db651d41ddffe8a to your computer and use it in GitHub Desktop.

Select an option

Save TanvirHasan19/455f0422d0ecccaf0db651d41ddffe8a to your computer and use it in GitHub Desktop.
Hide B2C-Only Product Category from Wholesale Users
define( 'WWPP_B2C_ONLY_CATEGORY_ID', 123 ); // Change this to your category ID
/**
* Option 2: Use category slug (alternative)
* Uncomment and set the slug if you prefer using slug instead of ID
*/
// define( 'WWPP_B2C_ONLY_CATEGORY_SLUG', 'your-category-slug' );
/**
* Get the B2C-only category ID
*
* @return int|false Category ID or false if not found
*/
function wwpp_get_b2c_only_category_id() {
// If using category slug
if ( defined( 'WWPP_B2C_ONLY_CATEGORY_SLUG' ) && ! empty( WWPP_B2C_ONLY_CATEGORY_SLUG ) ) {
$term = get_term_by( 'slug', WWPP_B2C_ONLY_CATEGORY_SLUG, 'product_cat' );
return $term ? $term->term_id : false;
}
// If using category ID
if ( defined( 'WWPP_B2C_ONLY_CATEGORY_ID' ) && ! empty( WWPP_B2C_ONLY_CATEGORY_ID ) ) {
$term = get_term( WWPP_B2C_ONLY_CATEGORY_ID, 'product_cat' );
return $term && ! is_wp_error( $term ) ? $term->term_id : false;
}
return false;
}
/**
* Check if current user is a wholesale user
*
* @return bool True if user has wholesale role, false otherwise
*/
function wwpp_is_wholesale_user() {
global $wc_wholesale_prices_premium;
if ( ! isset( $wc_wholesale_prices_premium ) ) {
return false;
}
$user_wholesale_role = $wc_wholesale_prices_premium->wwpp_wholesale_roles->getUserWholesaleRole();
return ! empty( $user_wholesale_role );
}
/**
* Filter product categories to hide B2C-only category from wholesale users
*
* @param array $terms Array of term objects
* @param array $taxonomies Array of taxonomies
* @param array $args Query arguments
* @param object $term_query WP_Term_Query object
* @return array Filtered array of terms
*/
function wwpp_hide_b2c_category_from_wholesale( $terms, $taxonomies, $args, $term_query ) {
// Only filter on frontend and for product_cat taxonomy
if ( is_admin() || ! in_array( 'product_cat', $taxonomies, true ) ) {
return $terms;
}
// Only filter for wholesale users
if ( ! wwpp_is_wholesale_user() ) {
return $terms;
}
// Get the B2C-only category ID
$b2c_category_id = wwpp_get_b2c_only_category_id();
if ( ! $b2c_category_id ) {
return $terms;
}
// Filter out the B2C-only category
$filtered_terms = array();
foreach ( $terms as $term ) {
if ( isset( $term->term_id ) && (int) $term->term_id !== (int) $b2c_category_id ) {
$filtered_terms[] = $term;
}
}
return $filtered_terms;
}
add_filter( 'get_terms', 'wwpp_hide_b2c_category_from_wholesale', 20, 4 );
/**
* Block access to B2C-only category page for wholesale users
* Redirects wholesale users away from the category page
*/
function wwpp_block_b2c_category_access_for_wholesale() {
// Only on frontend
if ( is_admin() ) {
return;
}
// Only for wholesale users
if ( ! wwpp_is_wholesale_user() ) {
return;
}
// Check if we're on a product category page
if ( ! is_product_category() ) {
return;
}
// Get the B2C-only category ID
$b2c_category_id = wwpp_get_b2c_only_category_id();
if ( ! $b2c_category_id ) {
return;
}
// Get current category ID
$current_cat_id = get_queried_object_id();
// If current category is the B2C-only category, redirect
if ( (int) $current_cat_id === (int) $b2c_category_id ) {
$redirect_url = apply_filters( 'wwpp_b2c_category_redirect_url', wc_get_page_permalink( 'shop' ) );
wp_safe_redirect( $redirect_url );
exit;
}
}
add_action( 'template_redirect', 'wwpp_block_b2c_category_access_for_wholesale', 10 );
/**
* Hide B2C-only category from navigation menus for wholesale users
*
* @param array $items Menu items
* @param array $args Menu arguments
* @return array Filtered menu items
*/
function wwpp_hide_b2c_category_from_menu( $items, $args ) {
// Only on frontend
if ( is_admin() ) {
return $items;
}
// Only for wholesale users
if ( ! wwpp_is_wholesale_user() ) {
return $items;
}
// Get the B2C-only category ID
$b2c_category_id = wwpp_get_b2c_only_category_id();
if ( ! $b2c_category_id ) {
return $items;
}
// Filter out menu items for the B2C-only category
foreach ( $items as $key => $item ) {
if ( isset( $item->type ) && 'taxonomy' === $item->type &&
isset( $item->object ) && 'product_cat' === $item->object &&
isset( $item->object_id ) && (int) $item->object_id === (int) $b2c_category_id ) {
unset( $items[ $key ] );
}
}
return $items;
}
add_filter( 'wp_nav_menu_objects', 'wwpp_hide_b2c_category_from_menu', 10, 2 );
/**
* Filter products in B2C-only category from product queries for wholesale users
*
* @param array $query_args Query arguments
* @return array Modified query arguments
*/
function wwpp_exclude_b2c_category_products_from_wholesale( $query_args ) {
// Only on frontend
if ( is_admin() ) {
return $query_args;
}
// Only for wholesale users
if ( ! wwpp_is_wholesale_user() ) {
return $query_args;
}
// Get the B2C-only category ID
$b2c_category_id = wwpp_get_b2c_only_category_id();
if ( ! $b2c_category_id ) {
return $query_args;
}
// Exclude products from the B2C-only category
if ( ! isset( $query_args['tax_query'] ) ) {
$query_args['tax_query'] = array();
}
$query_args['tax_query'][] = array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( $b2c_category_id ),
'operator' => 'NOT IN',
);
return $query_args;
}
add_filter( 'woocommerce_product_query', 'wwpp_exclude_b2c_category_products_from_wholesale', 10, 1 );
/**
* Filter products in B2C-only category from shortcode queries for wholesale users
*
* @param array $args Shortcode arguments
* @return array Modified arguments
*/
function wwpp_exclude_b2c_category_from_shortcodes( $args ) {
// Only on frontend
if ( is_admin() ) {
return $args;
}
// Only for wholesale users
if ( ! wwpp_is_wholesale_user() ) {
return $args;
}
// Get the B2C-only category ID
$b2c_category_id = wwpp_get_b2c_only_category_id();
if ( ! $b2c_category_id ) {
return $args;
}
// Exclude the B2C-only category
if ( ! isset( $args['tax_query'] ) ) {
$args['tax_query'] = array();
}
$args['tax_query'][] = array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => array( $b2c_category_id ),
'operator' => 'NOT IN',
);
return $args;
}
add_filter( 'woocommerce_shortcode_products_query', 'wwpp_exclude_b2c_category_from_shortcodes', 10, 1 );
/**
* Hide B2C-only category from WooCommerce category widgets for wholesale users
*
* @param array $args Widget arguments
* @return array Modified arguments
*/
function wwpp_hide_b2c_category_from_widgets( $args ) {
// Only on frontend
if ( is_admin() ) {
return $args;
}
// Only for wholesale users
if ( ! wwpp_is_wholesale_user() ) {
return $args;
}
// Get the B2C-only category ID
$b2c_category_id = wwpp_get_b2c_only_category_id();
if ( ! $b2c_category_id ) {
return $args;
}
// Exclude the B2C-only category
if ( ! isset( $args['exclude'] ) ) {
$args['exclude'] = array();
} elseif ( ! is_array( $args['exclude'] ) ) {
$args['exclude'] = array( $args['exclude'] );
}
$args['exclude'][] = $b2c_category_id;
$args['exclude'] = array_unique( $args['exclude'] );
return $args;
}
add_filter( 'woocommerce_product_categories_widget_args', 'wwpp_hide_b2c_category_from_widgets', 10, 1 );
/**
* Hide B2C-only category products from related products for wholesale users
*
* @param array $related_posts Related product IDs
* @param int $product_id Product ID
* @return array Filtered related product IDs
*/
function wwpp_exclude_b2c_category_from_related_products( $related_posts, $product_id ) {
// Only for wholesale users
if ( ! wwpp_is_wholesale_user() ) {
return $related_posts;
}
// Get the B2C-only category ID
$b2c_category_id = wwpp_get_b2c_only_category_id();
if ( ! $b2c_category_id ) {
return $related_posts;
}
// Filter out products in the B2C-only category
$filtered_posts = array();
foreach ( $related_posts as $post_id ) {
$product_cats = wp_get_post_terms( $post_id, 'product_cat', array( 'fields' => 'ids' ) );
if ( ! in_array( $b2c_category_id, $product_cats, true ) ) {
$filtered_posts[] = $post_id;
}
}
return $filtered_posts;
}
add_filter( 'woocommerce_output_related_products_args', 'wwpp_exclude_b2c_category_from_related_products', 10, 2 );
/**
* Block access to individual products in B2C-only category for wholesale users
*
* @param int $product_id Product ID
*/
function wwpp_block_b2c_category_product_access_for_wholesale( $product_id ) {
// Only on frontend
if ( is_admin() ) {
return;
}
// Only for wholesale users
if ( ! wwpp_is_wholesale_user() ) {
return;
}
// Get the B2C-only category ID
$b2c_category_id = wwpp_get_b2c_only_category_id();
if ( ! $b2c_category_id ) {
return;
}
// Check if product is in the B2C-only category
$product_cats = wp_get_post_terms( $product_id, 'product_cat', array( 'fields' => 'ids' ) );
if ( in_array( $b2c_category_id, $product_cats, true ) ) {
$redirect_url = apply_filters( 'wwpp_b2c_category_redirect_url', wc_get_page_permalink( 'shop' ) );
wp_safe_redirect( $redirect_url );
exit;
}
}
// Hook into product page access
add_action( 'template_redirect', function() {
if ( is_product() ) {
global $post;
if ( isset( $post->ID ) ) {
wwpp_block_b2c_category_product_access_for_wholesale( $post->ID );
}
}
}, 20 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment