要从子类别(子类别)中获取相关产品作为备用,当产品中只设置了一个产品类别时,请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 );
它也应该以同样的方式工作.