woocommerce - 현재 제품 카테고리의 최상위 카테고리를 얻으려면 어떻게 해야 합니까?
Woocommerce 제품이 있으며 해당 페이지에 제품에 할당된 카테고리의 최상위 범주를 표시해야 합니다.
- Main Product Category
-- Sub Product Category
--- Category Assigned to product
하나의 상품 카테고리에 표시하기 위해서는 "메인 상품 카테고리"의 아이디 또는 이름을 취득해야 합니다.
이미 다음을 시도했습니다.
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 후에 페이지가 로드되지 않습니다.이 시점에서 어떻게 해야 할지 모르겠어요
도와줘서 고마워요
또는 다음과 같은 경우가 있습니다.
$cat = get_the_terms( $product->ID, 'product_cat' );
foreach ($cat as $categoria) {
if($categoria->parent == 0){
echo $categoria->name;
}
}
많은 연구 끝에 이 문제를 해결할 방법을 알아냈다.이게 도움이 됐으면 좋겠어요.
솔루션:
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>';
}
}
여기 실제로 사용하는 솔루션이 있습니다.
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);
오래된 게시물인 것은 알지만, 현재 보고 있는 제품의 루트 카테고리를 취득해야 하는 비슷한 상황이 있었습니다.제가 생각할 수 있는 가장 간단한 해결책은 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
}
}
깨끗한 솔루션을 원하는 분들을 위해 제품 ID만 전달하면 ID, 이름, 링크(URL)라는 용어를 얻을 수 있도록 @Nicolas GRILET의 기능을 확장했습니다.
function get_parent_terms(int $product_id)
{
// Get all the terms associated with the product
$product_terms = get_the_terms($product_id, 'product_cat');
// Check if there are terms associated with the product
if ($product_terms) {
// Loop through $product_terms to find parent terms
foreach ($product_terms as $product_term) {
// Climb up the taxonomy hierarchy until parent found
while ($product_term->parent > 0) {
$product_term = get_term_by("id", $product_term->parent, "product_cat");
}
// Store parent terms as objects in array $parent_terms
$parent_terms[] = (object)[
'term_id' => $product_term->term_id,
'name' => $product_term->name,
'link' => get_term_link($product_term->term_id, 'product_cat')
];
}
// Return array of parent terms
return $parent_terms;
} else {
// Return false if there are no terms
return false;
}
}
그러면 제품이 속한 모든 최상위 범주의 배열이 제공됩니다.
제품에 최상위 카테고리가 1개밖에 없다고 가정하면 아래 예에서는 제품의 최상위 카테고리 ID, 이름 및 링크를 가져오는 방법을 보여 줍니다.
// Get the first top level category (object)
$top_level_cat = get_parent_terms($product_id)[0];
// Echo the ID
echo $top_level_cat->term_id;
// Echo the Name
echo $top_level_cat->name;
// Echo the Link/URL
echo $top_level_cat->link;
여기서 '카테고리'가 있는 경우아이디"
/*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
상위 부모 카테고리를 가져오려면 다음과 같이 하십시오.
$uncategorized_term_id = get_option( 'default_product_cat' );
언급URL : https://stackoverflow.com/questions/20777929/woocommerce-how-do-i-get-the-most-top-level-category-of-the-current-product-ca
'it-source' 카테고리의 다른 글
범용 브라우저를 지원하는 Clip-Path를 대체하는 방법 (0) | 2023.02.12 |
---|---|
AngularJs에서 외부 리소스를 로드하지 않습니다. (0) | 2023.02.12 |
jQuery 검증자 및 AJAX를 사용하는 사용자 지정 규칙 (0) | 2023.02.12 |
노드의 부정한 JSON.parse()를 안전하게 처리한다. (0) | 2023.02.12 |
요소 포커스를 각도 방향으로 설정 (0) | 2023.02.12 |