Detect whether a page is a product subcategory page?

is_category() only check the built in post category. Product categories are custom taxonomies. So you need to use is_tax() instead of is_category() and get_term() instead of get_category().

Check this example:-

function is_subcategory($cat_id = null) {
    if (is_tax('product_cat')) {

        if (empty($cat_id)){
            $cat_id = get_queried_object_id();
        }

        $cat = get_term(get_queried_object_id(), 'product_cat');
        if ( empty($cat->parent) ){
            return false;
        }else{
            return true;
        }
    }
    return false;
}

Also safe side instead of get_query_var() use get_queried_object_id()

Leave a Comment