web-dev-qa-db-fra.com

woocommerce - Comment puis-je obtenir la catégorie de premier niveau de la catégorie de produits actuelle

J'ai un produit Woocommerce et je dois afficher sur cette page la catégorie de premier niveau d'une catégorie attribuée au produit.

- Main Product Category
-- Sub Product Category
--- Category Assigned to product

Je dois obtenir l'ID ou le nom "Catégorie de produit principale" pour pouvoir l'afficher dans la catégorie de produit unique. 

J'ai déjà essayé de faire ce qui suit:

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

foreach ($terms as $term) {
$product_cat_id = $term->term_id;

$thetop_parent = woocommerce_get_term_top_most_parent( $product_cat_id , 'product_cat' );

echo $thetop_parent;
}

Mais cela n'a pas fonctionné du tout et cela a freiné le chargement de la page après woocomerce_get_term ... Je ne sais pas quoi faire à ce stade.

merci pour toute aide à ce sujet.

10
Gman

Après de nombreuses recherches, j'ai trouvé un moyen de résoudre ce problème. J'espère que cela aidera quelqu'un. 

solution:

global $post;
$prod_terms = get_the_terms( $post->ID, 'product_cat' );
foreach ($prod_terms as $prod_term) {

    // gets product cat id
    $product_cat_id = $prod_term->term_id;

    // gets an array of all parent category levels
    $product_parent_categories_all_hierachy = get_ancestors( $product_cat_id, 'product_cat' );  



    // This cuts the array and extracts the last set in the array
    $last_parent_cat = array_slice($product_parent_categories_all_hierachy, -1, 1, true);
    foreach($last_parent_cat as $last_parent_cat_value){
        // $last_parent_cat_value is the id of the most top level category, can be use whichever one like
        echo '<strong>' . $last_parent_cat_value . '</strong>';
    }

}
22
Gman

ou peut-être ceci:

$cat = get_the_terms( $product->ID, 'product_cat' );

foreach ($cat as $categoria) {
if($categoria->parent == 0){
   echo $categoria->name;
}
}
21
Mauro

Je sais que ceci est un ancien message, mais j’ai eu une situation similaire dans laquelle nous devions obtenir la catégorie racine du produit actuel en cours de visualisation. La solution la plus simple à laquelle je pouvais penser était de regarder comment WooCommerce faisait sa chapelure, et ce morceau de code est ce qui a fait le tour pour moi:

if ( is_product() ) {
    global $post;
    $terms = wc_get_product_terms( $post->ID, 'product_cat', array( 'orderby' => 'parent', 'order' => 'DESC' ) );
    if ( ! empty( $terms ) ) {
        $main_term = $terms[0];
        $ancestors = get_ancestors( $main_term->term_id, 'product_cat' );
        if ( ! empty( $ancestors ) ) {
            $ancestors = array_reverse( $ancestors );
            // first element in $ancestors has the root category ID
            // get root category object
            $root_cat = get_term( $ancestors[0], 'product_cat' );
        }
        else {
            // root category would be $main_term if no ancestors exist
        }
    }
    else {
        // no category assigned to the product
    }
}
6
thorne51

Voici la solution que j'utilise réellement

function get_parent_terms($term) {
    if ($term->parent > 0){
        $term = get_term_by("id", $term->parent, "product_cat");
        return get_parent_terms($term);
    }else{
        return $term->term_id;
    }
}
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
$Root_Cat_ID = get_parent_terms($cat_obj);
4
Nicolas GRILLET

Ici si vous avez un "category_ID"

/*If you dont have a category ID use this: 
*       $category_ID = get_queried_object()->term_id;
* This will get you the current category_ID 
*/

$termid = get_term($category_ID, 'product_cat' );
    if($termid->parent > 0) 
    {                       // get the parent's hierarchy.
        $cat_hierachy = get_ancestors( $category_ID, 'product_cat' );  
        return end($cat_hierachy); // returns the Level-1 category_ID
    }
    return $category_ID; // Has no parent so return THIS category_ID  Level-1
0