Показывать подкатегории Woocommerce на одной странице по слагу или идентификатору их родителей - PullRequest
0 голосов
/ 10 февраля 2020

Я пытаюсь добавить Publisher, Topi c и Author к одному продукту с помощью категорий / подкатегорий. Вот как это выглядит после нескольких часов кодирования / и копирования (очень часто sh с WooCommerce tbh) look of the current state

Это то, что я получаю, но оно показывает ВСЕ подкатегории, не только те, которые связаны с продуктом, это код, который я использую

    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>';
        }

Но я не могу заставить все работать так, как я хочу, и получить подкатегории одного продукта в в этом примере должны быть только Духовность, SOUNDS TRUE IN C (только sub cat в Publishers) и Аллан Уоттс.

Буду признателен за любую помощь!

1 Ответ

0 голосов
/ 11 февраля 2020

У меня есть рабочий код (он не выглядит красиво, я знаю, но это лучшее, что я мог сделать, и он делает свое дело.

add_action('woocommerce_single_product_summary','monolith_cat_scan', 31);

function monolith_cat_scan() {

global $post;

$cats = get_the_terms( $post->ID, 'product_cat' );

if ( ! empty( $cats ) ) {

        foreach ( $cats as $term ) {

        if( $term->parent == 30 ) { 
      echo '<p>Topic : <a href="' . site_url() . '/product-category/' . $term->slug . '">' . $term->name . '</a>';
   }

        }

}

}
add_action('woocommerce_single_product_summary','monolith_cat_scan2', 32);

function monolith_cat_scan2() {

global $post;

$cats = get_the_terms( $post->ID, 'product_cat' );

if ( ! empty( $cats ) ) {

        foreach ( $cats as $term ) {

        if( $term->parent == 31 ) { 
      echo '<p>Author : <a href="' . site_url() . '/product-category/' . $term->slug . '">' . $term->name . '</a>';
   }

        }

}

}

add_action('woocommerce_single_product_summary','monolith_cat_scan3', 33);

function monolith_cat_scan3() {

global $post;

$cats = get_the_terms( $post->ID, 'product_cat' );

if ( ! empty( $cats ) ) {

        foreach ( $cats as $term ) {

   // If parent cat ID = 116 echo subcat name...
        if( $term->parent == 32 ) { 
      echo '<p>Publisher : <a href="' . site_url() . '/product-category/' . $term->slug . '">' . $term->name . '</a>';
   }

        }

}

}
...