0
votes

I created a custom taxonomy book-author for my Woocommerce store, and now I'm trying to conjoint an archive template for it to display frontend like normal Woo archive. Yesterday I found this code, which helped me bring the taxonomy out of the 404 error when clicked, but it returned a shop page with No product notice (though I'd clicked on one of the existed taxonomies).

The point is, book-author taxonomy is a mother of small tags, or "authors", so I need to fix this tag or find a way to make it universal to the mother book-author and all its kids/authors.

add_filter('template_include', 'team_set_template');
function team_set_template( $template ){
  if(is_tax('book-author')) :
    $taxonomy = 'book-author';
    $term = get_query_var($taxonomy);
    $prod_term = get_terms($taxonomy, 'slug='.$term.''); 
    $term_slug = $prod_term[0]->slug;
    $t_id = $prod_term[0]->term_id;
    $term_meta = get_option( "taxonomy_$t_id" );
    $term_meta['bookauthor_access_pin'];  

    wc_get_template( 'archive-product.php' );

 else : 

    wc_get_template( 'archive-product.php' );

 endif; 

}

I've tried copying archive-product.php, renaming it taxonomy-book-author.php and putting it in my child theme folder. This seems to be a more better approach, but there was no result - still 404.

The reason why book-author is a tag, not a category because there is no hierarchy for an author. And I know there's plugin for this (Toolset), but they upgraded there free version to paid ones so I'm trying to find a more manual and permanent way.

Thank you in advance, guys.

1

1 Answers

0
votes

There is many errors just when reviewing and trying your code…

Note: A filter hooked function needs always to return something.

In your code: - get_query_var($taxonomy) will return always a term "slug" - $term_meta['bookauthor_access_pin']; is not usefull and I really don't know what is for.

You say "The reason why book-author is a tag, not a category"… If you have created a custom taxonomy book-author, this can't be a product category or either a product tag (woocommerce custom taxonomies)…

So you should try the following code instead (without any guaranty, as you don't provide all your related code, and this can't be tested):

add_filter('template_include', 'team_set_template');
function team_set_template( $template ){

    $taxonomy = 'book-author';

    if( ! is_tax( $taxonomy ) ) return $template;

    $term_slug = get_query_var($taxonomy);

    if ( empty( $term_slug ) ) return $template;

    $term = get_term_by('slug', $term_slug, $taxonomy );

    if ( is_wp_error( $term ) ) return $template;

    $term_id = $prod_term->term_id;
    $term_meta = get_option( 'taxonomy_'. $term_id );

    // $term_meta['bookauthor_access_pin']; //  ???

    return wc_locate_template( 'archive-product.php' );
}