我有一个Woocommerce产品,我需要在该页面上显示分配给该产品的类别的最顶级类别
- Main Product Category
-- Sub Product Category
--- Category Assigned to product
我需要获取“主要产品类别”的ID或名称,以便我可以在单个产品类别中显示它。
我已经尝试过以下操作:
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;
}
但它根本没用,它在woocomerce_get_term之后加载页面......我不知道该怎么做它</ p>
感谢您的帮助。
答案 0 :(得分:24)
经过大量研究后,我找到了解决这个问题的方法。我希望这会对某人有所帮助。
溶液:
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>';
}
}
答案 1 :(得分:22)
或者这个:
$cat = get_the_terms( $product->ID, 'product_cat' );
foreach ($cat as $categoria) {
if($categoria->parent == 0){
echo $categoria->name;
}
}
答案 2 :(得分:7)
我知道这是一个旧帖子,但我遇到了类似的情况,我们需要查看当前产品的根类别。我能想到的最简单的解决方案是看看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
}
}
答案 3 :(得分:5)
这是我实际使用的解决方案
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 :(得分:0)
如果你有一个“category_ID”
err
答案 5 :(得分:0)
这是我开发使用的功能。
/**
* 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;
}
因此,您可以使用此方法获取当前产品的顶级类别
$top_level_term = get_product_top_category($post->ID);
此代码的局限性在于,它可以返回任何产品的多个顶级类别。为了解决这个问题,我使用LIMIT 1
仅返回适合我的单个类别。