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

Asked

Viewed 205 times

0

Personal as I take the parent category of a registered product, as per ex:

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

I am using a webhook in my functions.php, it shows the categories but not being the parent category the first to appear, because I think because it is in alphabetical order it does this. Follows 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 );

Is it possible to show only the parent category? Thanks in advance for the help!

  • Put the structure of your table of categories to help. But trying to anticipate, if your table of categories has auto-relationships, just in your "SELECT" categories you only search for those that have the foreign key (auto-relationship) blank (null)

2 answers

1

Assuming that the Parent Category be the highest category in the hierarchy (i.e., she herself does not have a father), a method that seeks the highest in the hierarchy solves her 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 defined this function, you can change your method wpa89819_wc_single_product() for:

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 to your logic.

Source

  • 1

    Caio has not tested his solution, because I have found another way to do what I want and I will post now. I thank you for your time and still today I will be doing a test with your solution. Thanks!

0

After talking to some people who work with WP we have come up with a solution that I liked and will share with you. I appreciate who took the time to analyze my need and somehow tried to help. Follows 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 );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.