使用具有 3 级层次结构的自定义分类法选择 woocommerce 相关产品
P粉037215587
P粉037215587 2024-04-06 20:05:30
0
1
783

我有一家 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;
}

它应该可以工作。

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板