我有以下代码从this question应用,以显示WooCommerce相关产品的排名数学主要类别,当产品中 Select 了2个或更多类别.

function related_products_from_rank_math_primary_category( $related_posts, $product_id, $args  ) {
    $taxonomy   = 'product_cat';
    $categories = wp_get_post_terms($product_id, $taxonomy, array('fields' => 'ids') ); // Get product categories

    // Check if the product has more than one product category
    if( count( $categories ) > 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 );
        $primary_cat_info = get_term_by( 'id', $primary_cat_id, $taxonomy );

        // Get ids of just the posts in this category. We only want related posts from the primary category.
        $related_posts = wc_get_products( array(
            'status'        => 'publish',
            'category'      => array( $primary_cat_info->slug ),
            'return'        => 'ids',
            'exclude'       => array( $product_id ),
            'visibility'    => 'catalog',
            'limit'         => -1,
        ) );
    }
    return $related_posts;
}
add_filter( 'woocommerce_related_products', 'related_products_from_rank_math_primary_category', 10, 3 );

我正在try 为只 Select 了一个类别(标准或主要)的产品添加一个备用类别,以便使用子类别来显示相关产品.

作为测试,我已经试了this code,但它似乎过时了,而且没有展示子类别的产品.

此外,我还try 按照同一问题中的建议覆盖WooCommerce Related.php模板文件. 这起作用了,但阻止了上面的Rank Math初级类别代码工作.

我还没能想出如何在Rank Math初级分类代码中使用模板覆盖的一部分.

任何帮助都将不胜感激.

推荐答案

要从子类别(子类别)中获取相关产品作为备用,当产品中只设置了一个产品类别时,请try 以下重新访问的代码:

// Rank Math SEO - Use Primary Category for Related Products
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  ) {
    $taxonomy     = 'product_cat';
    $category_ids = wp_get_post_terms($product_id, $taxonomy, array('fields' => 'ids') ); 
    $term_slugs   = array(); // Initializing   

    if( count($category_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( $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 );
        $term_slugs[]   = get_term_by( 'id', $primary_cat_id, $taxonomy )->slug; // get the slug from the term Id
    }

    if ( count($term_slugs) > 0 ) {
        // Product query: Get product Ids
        $related_posts = wc_get_products( array(
            'status'        => 'publish',
            'category'      => $term_slugs,
            'return'        => 'ids',
            'exclude'       => array( $product_id ),
            'visibility'    => 'catalog',
            'limit'         => -1,
        ) );
    }
    return $related_posts;
}

它应该会像预期的那样工作.


当只有一个产品类别时,如果要将子术语ID与该当前产品类别合并,请在代码中替换:

foreach ( $children_ids as $tem_id ) {

使用

foreach ( array_merge( $category_ids, $children_ids ) as $tem_id ) {

添加

正如行动小组在 comments (separate parts)中所要求的那样:

function related_products_from_rank_math_primary_category( $related_posts, $product_id, $args  ) {
    $taxonomy     = 'product_cat';
    $category_ids = wp_get_post_terms($product_id, $taxonomy, array('fields' => 'ids') ); 

    // If there is only one product category
    if( count($category_ids) == 1 ) {
        $term_slugs   = array(); // Initializing  
        // Get children categories
        $children_ids = get_term_children( reset($category_ids), $taxonomy );
        // Loop through children terms Ids
        foreach ( array_merge( $category_ids, $children_ids ) as $tem_id ) {
            $term_slugs[] = get_term_by( 'id', $tem_id, $taxonomy )->slug; // get the slug from each term Id
        }
         // Product query: Get product Ids
        $related_posts = wc_get_products( array(
            'status'        => 'publish',
            'category'      => $term_slugs,
            'return'        => 'ids',
            'exclude'       => array( $product_id ),
            'visibility'    => 'catalog',
            'limit'         => -1,
        ) );
    } 

    // If the product has more than one product category
    if( 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 );
        $primary_cat_info = get_term_by( 'id', $primary_cat_id, $taxonomy );

        // Get ids of just the posts in this category. We only want related posts from the primary category.
        $related_posts = wc_get_products( array(
            'status'        => 'publish',
            'category'      => array( $primary_cat_info->slug ),
            'return'        => 'ids',
            'exclude'       => array( $product_id ),
            'visibility'    => 'catalog',
            'limit'         => -1,
        ) );
    }
    return $related_posts;
}
add_filter( 'woocommerce_related_products', 'related_products_from_rank_math_primary_category', 10, 3 );

它也应该以同样的方式工作.

Php相关问答推荐

如何在Livewire中验证多个 Select 字段

从WooCommerce邮箱通知中的订单详细信息中删除产品列表

无法在Laravel/Vue停靠容器中启动MySQL

通过注册在Laravel中分配角色

如何在Foreach语句中使用php和li按降序排序?

根据产品类型显示或隐藏WooCommerce管理产品自定义字段

多行PHP属性声明中是否允许单行注释

目标类[Set_Locale]不存在.拉威尔

PHP API - 本地主机中的 SSL 证书问题

如何在 Laravel 迁移中使用日期时间和天数?

无法在 Laravel 中实例化抽象类 ProductAbstract

避免在 WooCommerce 中多次触发挂钩函数

在 Laravel、Vuejs 和 Inertia 中配置 TypeScript 的问题

在 WordPress 中使用 $wpdb 运行 MySQL 事务是否安全?

正则表达式抓取递归括号的内容

遇到特定键时修改二维数组以创建嵌套数据集

从 CSV 文件 seeder 数据库时的额外行

如何使用先前 SELECT 的结果来 SELECT SQL 数据

如何将在同一台服务器上运行的 2 个 PHP 应用程序与使用公共会话分开

将加密/解密函数从 PHP 转换为 NodeJS