php woocommerce - 如何获得当前产品类别的最顶级类别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20777929/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:33:12  来源:igfitidea点击:

woocommerce - How do I get the most top level category of the current product category

phpwordpresswoocommerce

提问by Gman

I have a Woocommerce product and I need to display on that page the Most top level category of a category assigned to the product

我有一个 Woocommerce 产品,我需要在该页面上显示分配给该产品的类别的最高级别类别

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

I need to get the ID or name of "Main Product Category" so that I can display it in the single product category.

我需要获取“Main Product Category”的ID或名称,以便我可以在单个产品类别中显示它。

I already tried doing the following:

我已经尝试执行以下操作:

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

But It didn't worked at all and it brakes the page from loading after woocomerce_get_term... I'm not sure what to do at this point It

但它根本不起作用,它在 woocomerce_get_term 之后阻止页面加载......我不知道此时该怎么做它

thanks for any help on this.

感谢您对此的任何帮助。

回答by Gman

After a lot of research I figured a way to solve this. I hope this will help someone.

经过大量研究,我找到了解决此问题的方法。我希望这会帮助某人。

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

}

回答by Mauro

or maybe this:

或者这个:

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

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

回答by thorne51

I know this is an old post, but I had a similar situation where we needed to get the root category of the current product being viewed. The simplest solution I could think of was to look at how WooCommerce does its breadcrumbs, and this piece of code is what did the trick for me:

我知道这是一篇旧帖子,但我遇到了类似的情况,我们需要获取当前正在查看的产品的根类别。我能想到的最简单的解决方案是看看 WooCommerce 是如何做面包屑的,这段代码对我有用:

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

回答by Nicolas GRILLET

Here the solution i actually use

这是我实际使用的解决方案

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);

回答by Vaibhav Bhanushali

This is the function I've developed to use.

这是我开发使用的功能。

/**
 * Get product's top category
 * @param int $pid. The product ID
 *
 * @return int. Returns product categories parent ID
 */
 function get_product_top_category($pid) {
    global $wpdb;
    $cat_id = $wpdb->get_var('SELECT b.term_id FROM '.$wpdb->prefix.'term_relationships a, '.$wpdb->prefix.'term_taxonomy b WHERE a.object_id = "'.$pid.'" AND a.term_taxonomy_id = b.term_taxonomy_id AND b.parent = "0" AND b.taxonomy = "product_cat" LIMIT 1');
    return $cat_id;
 }

So you can fetch the top category of current product by using this

因此,您可以使用此获取当前产品的顶级类别

$top_level_term = get_product_top_category($post->ID);

A limitation of this code are that it could return multiple top level categories of any product. To combat that, I used LIMIT 1to return only single category which suits fine for me.

此代码的一个限制是它可以返回任何产品的多个顶级类别。为了解决这个问题,我过去LIMIT 1只返回适合我的单个类别。

回答by Siddhartha Chowdhury

Here if u have the a "category_ID"

在这里,如果你有一个“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