• Resolved Johannes

    (@romantwentyeight)


    I am trying to add Publisher, Topic and Author to a Single Product with help of categories/subcategories. This is how it looks after hours of coding/and copying (very fresh with WooCommerce tbh) look of the current state

    This is what I am getting, but it shows ALL subcategories, not only the ones associated to the Product, this is the code I am using

    ` function get_product_subcategories_list( $category_slug ){
    $terms_html = array();
    $taxonomy = ‘product_cat’;
    // Get the product category (parent) WP_Term object
    $parent = get_term_by( ‘slug’, $category_slug, $taxonomy );
    // Get an array of the subcategories IDs (children IDs)
    $children_ids = get_term_children( $parent->term_id, $taxonomy );

    // Loop through each children IDs
    foreach($children_ids as $children_id){
    $term = get_term( $children_id, $taxonomy ); // WP_Term object
    $term_link = get_term_link( $term, $taxonomy ); // The term link
    if ( is_wp_error( $term_link ) ) $term_link = ”;
    // Set in an array the html formated subcategory name/link
    $terms_html[] = ‘<a href=”‘ . esc_url( $term_link ) . ‘” rel=”tag” class=”‘ . $term->slug . ‘”>’ . $term->name . ‘</a>’;
    }
    return ‘<span class=”subcategories-‘ . $category_slug . ‘”>’ . implode( ‘, ‘, $terms_html ) . ‘</span>’;
    }
    add_action(‘woocommerce_single_product_summary’,’monolith_cat_scan’, 31);
    function monolith_cat_scan() {
    echo ‘<p>Topic : ‘;
    echo get_product_subcategories_list( ‘topics’ );
    echo ‘</p>’;
    echo ‘<p>Publisher : ‘;
    echo get_product_subcategories_list( ‘publishers’ );
    echo ‘</p>’;
    echo ‘<p>Author: ‘;
    echo get_product_subcategories_list( ‘authors’ );
    echo ‘</p>’;
    }`
    But I can’t get the whole thing to work like I want to and get the subcategories of the Single Product, in this example only Spirituality, SOUNDS TRUE INC (only sub cat in Publishers), and Allan Watts should be there.

    I also posted it on Stackoverflow, maybe someone can help here!

    https://stackoverflow.com/questions/60155915/show-woocommerce-subcategories-on-single-page-by-their-parents-slug-or-id

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Show Woocommerce subcategories on single page by their parents slug or ID’ is closed to new replies.