w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
Woocommerce: Can i restrict users from directly accessing a product category and its products?

I have done something and it is working fine, maybe it is useful for someone else. Here it is

function disable_free_offers_access() {
    global $wp_query, $post;
    $tax = 'product_cat';
    $term = 'coffee-tea';
    $post_type = 'product';
    $term_obj = get_term_by('slug', $term, $tax);
    $termchildren = get_term_children($term_obj->term_id, $tax);

    if (is_tax()) {
        if (is_tax($tax, $term) ||
term_is_ancestor_of($term_obj->term_id,
$wp_query->get_queried_object()->term_id, $tax)) {
            load_template(TEMPLATEPATH . '/404.php');
            exit;
        }
    } else if (is_singular($post_type)) {
        $terms = wp_get_post_terms($post->ID, $tax);
        if (!empty($terms))
            foreach ($terms as $t) {
                if ($t->term_id == $term_obj->term_id ||
in_array($t->term_id, $termchildren)) {
                    load_template(TEMPLATEPATH . '/404.php');
                    exit;
                }
            }
    }
}

add_action('template_redirect', 'disable_free_offers_access');




© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.