使用具有 3 級層次結構的自訂分類法選擇 woocommerce 相關產品
P粉037215587
P粉037215587 2024-04-06 20:05:30
0
1
864

我有一家 woocommerce 商店,其自訂分類為「運動」。此分類有三個等級 - 父級、子級、子級 - 例如:室內運動 > 競技場運動 > 籃球。如果使用者查看籃球項目,那麼我希望相關產品優先顯示其他籃球項目,然後在沒有足夠的籃球項目時回退到 Arena Sport 項目。因此,首先檢查最低級別 - Sub-Child,然後是 Child,然後是 Parent。

此外,我使用 RankMath,並且可以將分類術語設定為「主要」術語。因此,在上面的範例中,主要術語是籃球。主要術語幾乎總是子子術語,但也可能是子術語。

我結合了其他兩個問題的回答,程式碼的工作原理是從運動分類中選擇相關產品。但它只看子級別,不考慮子級別。因此,在上面的範例中,它選擇 Arena Sports 中的項目,而不優先考慮籃球。

我應該進行哪些更改才能確保相關產品看到“主要”分類術語,然後首先查找具有該術語的產品,然後(如有必要)在術語層次結構的下一級中查找?

感謝您提供的任何幫助或建議。

我在起草程式碼時參考了這兩篇文章:

以兒童類別劃分的 WooCommerce 相關產品作為排名數學主要類別的後備

使用自訂分類法在 Woocommerce 中選擇相關產品

這是我目前正在使用的程式碼:

add_filter( 'woocommerce_related_products', 'related_products_from_rankmath_primary_esporte_taxonomy', 10, 3 );
    function related_products_from_rankmath_primary_esporte_taxonomy( $related_posts, $product_id, $args ) {
        $taxonomy     = 'sport'; 
        $term_ids     = wp_get_post_terms( $product_id, $taxonomy, array( 'fields' => 'ids' ) ); 
        $term_slugs   = array(); 
  
 if( count($term_ids) == 1 ) {
        // Get children categories
        $children_ids = get_term_children( reset($category_ids), $taxonomy );
        // Loop through children terms Ids
        foreach ( $children_ids as $tem_id ) {
            $term_slugs[] = get_term_by( 'id', $tem_id, $taxonomy )->slug; // get the slug from each term Id
        }
    } 
    elseif( count( $term_ids ) > 1 ) {
        // Get the primary taxonomy/term as saved by Rank Math SEO
        $primary_tax_id = get_post_meta( $product_id, 'rank_math_primary_taxonomy', true );
        $term_slugs[]   = get_term_by( 'id', $primary_tax_id, $taxonomy )->slug; // get the slug from the term Id
    }
        
        
        if ( count( $term_ids ) > 0 ) {
            foreach ( $term_ids as $term_id ) {
                $term_slugs[] = get_term_by( 'id', $term_id, $taxonomy )->slug; 
    
                // Gets the IDs of child terms
                $children_ids = get_term_children( $term_id, $taxonomy );
    
                foreach ( $children_ids as $child_id ) {
                    $term_slugs[] = get_term_by( 'id', $child_id, $taxonomy )->slug; // Gets the slug of each child term
                }
            }
   
   
            $related_posts = wc_get_products( array(
                'status'        => 'publish',
                'tax_query'     => array(
                    array(
                        'taxonomy' => $taxonomy,
                        'field'    => 'slug',
                        'terms'    => $term_slugs,
                    ),
                ),
                'return'        => 'ids',
                'exclude'       => array( $product_id ),
                'visibility'    => 'catalog',
                'limit'         => -1,
            ) );
        }

    
    
        return $related_posts;
    }

P粉037215587
P粉037215587

全部回覆(1)
P粉029327711

嘗試以下操作(已評論):

// Utility function: Get related product Ids with a custom tax query
function get_related_posts_custom_query( $term_slugs, $taxonomy, $product_id ) {
    return wc_get_products( array(
        'limit'         => -1,
        'status'        => 'publish',
        'exclude'       => array( $product_id ),
        'visibility'    => 'catalog',
        'tax_query'     => array(
            array(
                'taxonomy' => $taxonomy,
                'field'    => 'slug',
                'terms'    => $term_slugs,
            ),
        ),
        'return'        => 'ids',
    ) );
}

add_filter( 'woocommerce_related_products', 'related_products_from_rank_math_primary_category', 10, 3 );
function related_products_from_rank_math_primary_category( $related_posts, $product_id, $args  ) {
    // Get product categories set for the product
    $category_ids = wp_get_post_terms($product_id, 'product_cat', array('fields' => 'ids') ); 
    $term_slugs   = array(); // Initializing   

    // 1). Only one product category => Fallback (Query products from "Sport" taxonomy)
    if( count($category_ids) == 1 ) {
        $taxonomy      = 'sport';
        // Get "Sport" the term set in the product
        $sport_ids     = wp_get_post_terms($product_id, $taxonomy, array('fields' => 'ids') ); 
        $term          = get_term_by( 'id', reset($sport_ids), $taxonomy );
        $term_slugs[]  = $term ->slug;
        // Get related products from the "Sport" term
        $related_posts = get_related_posts_custom_query( $term_slugs, $taxonomy, $product_id );

        // IF there is not enough related products: Add the CHILDREN terms
        if ( $related_posts  0 ) {
                foreach ( $children_ids as $tem_id ) {
                    $term_slugs[] = get_term_by( 'id', $tem_id, $taxonomy )->slug; // get the slug from each term Id
                }
                // Get related products from the "Sport" terms
                $related_posts = get_related_posts_custom_query( $term_slugs, $taxonomy, $product_id );
            }
            // IF there is not enough related products: Add the PARENT term
            if ( $related_posts parent, $taxonomy );
                $term_slugs[] = $parent ->slug;
                // Get related products from the "Sport" terms
                return get_related_posts_custom_query( $term_slugs, $taxonomy, $product_id );
            } else {
                return $related_posts;
            }
        } else {
            return $related_posts;
        }
    } 
    // 2). More than one product categories => Rank Math SEO
    elseif( count( $category_ids ) > 1 ) {
        // Get the primary category/term as saved by Rank Math SEO
        $primary_cat_id = get_post_meta( $product_id, 'rank_math_primary_product_cat', true );
        $taxonomy       = 'product_cat';
        $term_slugs[]   = get_term_by( 'id', $primary_cat_id, $taxonomy )->slug; // get the slug from the term Id
        // Get related products from the category terms via Rank Math SEO
        return get_related_posts_custom_query( $term_slugs, $taxonomy, $product_id );
    }
    return $related_posts;
}

它應該可以工作。

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板