Untitled

mail@pastecode.io avatar
unknown
plain_text
10 days ago
3.0 kB
1
Indexable
Never

/**
 * Hide specific product categories for a given user
 */
function hide_product_categories_for_user($q) {
    // Check if we're on the shop page, product archive, or search results
    if (is_shop() || is_product_category() || is_product_tag() || is_search()) {
        // ID of the user you want to hide the categories from
        $user_id = 123; // Replace with the actual user ID
        
        // IDs of the categories you want to hide
        $category_ids = array(456, 457, 458, 459, 460, 461, 462, 463); // Replace with your actual category IDs
        
        if (get_current_user_id() == $user_id) {
            $tax_query = $q->get('tax_query');
            
            if (!is_array($tax_query)) {
                $tax_query = array();
            }
            
            $tax_query[] = array(
                'taxonomy' => 'product_cat',
                'field' => 'term_id',
                'terms' => $category_ids,
                'operator' => 'NOT IN'
            );
            
            $q->set('tax_query', $tax_query);
        }
    }
}
add_action('woocommerce_product_query', 'hide_product_categories_for_user');
/**
 * Redirect user from hidden product pages
 */
function redirect_user_from_hidden_products() {
    // Check if we're on a single product page
    if (is_product()) {
        global $post;
        
        // ID of the user you want to hide the categories from
        $user_id = 123; // Replace with the actual user ID
        
        // IDs of the categories you want to hide
        $category_ids = array(456, 457, 458, 459, 460, 461, 462, 463); // Replace with your actual category IDs
        
        if (get_current_user_id() == $user_id) {
            if (has_term($category_ids, 'product_cat', $post->ID)) {
                wp_redirect(get_permalink(wc_get_page_id('shop')));
                exit;
            }
        }
    }
}
add_action('template_redirect', 'redirect_user_from_hidden_products');
/**
 * Exclude categories from WooCommerce term queries
 */
function exclude_categories_from_term_queries($args, $taxonomies) {
    // Only modify args for product category queries
    if (in_array('product_cat', $taxonomies)) {
        // ID of the user you want to hide the categories from
        $user_id = 123; // Replace with the actual user ID
        
        // IDs of the categories you want to hide
        $category_ids = array(456, 457, 458, 459, 460, 461, 462, 463); // Replace with your actual category IDs
        
        if (get_current_user_id() == $user_id) {
            if (!isset($args['exclude'])) {
                $args['exclude'] = array();
            }
            elseif (!is_array($args['exclude'])) {
                $args['exclude'] = array($args['exclude']);
            }
            
            $args['exclude'] = array_merge($args['exclude'], $category_ids);
        }
    }
    
    return $args;
}
add_filter('get_terms_args', 'exclude_categories_from_term_queries', 10, 2);
Leave a Comment