Add in the title of the product the parent category of the registered product

0

Personal as I get the parent category of a product registered, such as eg:

Categoria Pai
  -categoria filho 1
    -produto cadastrado 1
    -produto cadastrado 2
  -categoria filho 2
  -categoria filho 3

I'm using a webhook in my functions.php, it shows the categories but the parent category is not the first one to appear because I think it should be in alphabetical order so it does. Follow the code:

  // adiciona no titulo a categoria do produto
  function wpa89819_wc_single_product(){

    $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );

    if ( $product_cats && ! is_wp_error ( $product_cats ) ){
      while (!empty($product_cats)) {
        $single_cat = array_shift( $product_cats ); ?>

        <h2 itemprop="name" class="product_category_title"><span><?php echo $single_cat->name; ?></span></h2>

      }
     } 
    }
add_action( 'woocommerce_single_product_summary', 'wpa89819_wc_single_product', 2 );

Can you just show the parent category? Thanks in advance for the help!

    
asked by anonymous 25.06.2015 / 03:03

2 answers

1

Assuming the Parent Category is the highest category in the hierarchy (i.e., it does not have a parent itself), a method that searches the highest in the hierarchy solves your problem. Something like

function get_top_categoria($term_id, $taxonomy){

    $pai = get_term_by( 'id', $term_id, $taxonomy);

    while ($pai->parent != '0'){
        $term_id = $pai->parent;

        $pai  = get_term_by( 'id', $term_id, $taxonomy);
    }
    return $pai;
}

Once you set this function, you can change your wpa89819_wc_single_product() method to:

function wpa89819_wc_single_product(){

    $categorias_pai = array();

    $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );

    if ( $product_cats && ! is_wp_error ( $product_cats ) ){
        foreach($product_cats as $product_cat){
             $top_pai= get_top_categoria( $term->term_id, 'work_type' );
             if(!in_array($top_pai, $categorias_pai){
                 $categorias_pai = $top_pai;
             }
        }
    }

    //Aqui você tem o array com as categorias, e constrói o HTML como quiser
    foreach($categorias_pai as $categoria){
       //....
    }
}   

I did a simple test here, and the idea is basically this. You have to adapt just for your logic.

Font

    
26.06.2015 / 15:49
0

After talking with some people who work with WP we come up with a solution that I like and I'll share with you. Thank you who took the time to analyze this need and somehow tried to help. Here's the solution:

function wpa89819_wc_single_product(){

  $product_cats = wp_get_post_terms( get_the_ID(), 'product_cat' );
   $cat_filha = array();
   $cat_pai = array();
   if ( $product_cats && ! is_wp_error ( $product_cats ) ){
      while (!empty($product_cats)) {
         $single_cat = array_shift( $product_cats );
         if ($single_cat->parent){
             $cat_filha[] = $single_cat->name;
         } else {
             $cat_pai[] = $single_cat->name;
        }
      }
      ?>
    <h2 itemprop="name" class="product_category_title"><?php echo $cat_pai[0]; ?></h2>
    <h1 itemprop="name" class="product_title entry-title"><?php echo $cat_filha[0]?></h1>
    <?php
    }
}

add_action( 'woocommerce_single_product_summary', 'wpa89819_wc_single_product', 2 );
    
06.07.2015 / 21:52