我有一家 woocommerce 商店,其自定義分類為“運(yùn)動(dòng)”。該分類具有三個(gè)級(jí)別 - 父級(jí)、子級(jí)、子級(jí) - 例如:室內(nèi)運(yùn)動(dòng) > 競(jìng)技場(chǎng)運(yùn)動(dòng) > 籃球。如果用戶查看籃球項(xiàng)目,那么我希望相關(guān)產(chǎn)品優(yōu)先顯示其他籃球項(xiàng)目,然后在沒有足夠的籃球項(xiàng)目時(shí)回退到 Arena Sport 項(xiàng)目。因此,首先檢查最低級(jí)別 - Sub-Child,然后是 Child,然后是 Parent。
此外,我使用 RankMath,并且可以將分類術(shù)語(yǔ)設(shè)置為“主要”術(shù)語(yǔ)。因此,在上面的示例中,主要術(shù)語(yǔ)是籃球。主要術(shù)語(yǔ)幾乎總是子子術(shù)語(yǔ),但也可能是子術(shù)語(yǔ)。
我結(jié)合了其他兩個(gè)問題的回答,代碼的工作原理是從運(yùn)動(dòng)分類中選擇相關(guān)產(chǎn)品。但它只看子級(jí)別,不考慮子子級(jí)別。因此,在上面的示例中,它選擇 Arena Sports 中的項(xiàng)目,而不優(yōu)先考慮籃球。
我應(yīng)該進(jìn)行哪些更改才能確保相關(guān)產(chǎn)品看到“主要”分類術(shù)語(yǔ),然后首先查找具有該術(shù)語(yǔ)的產(chǎn)品,然后(如有必要)在術(shù)語(yǔ)層次結(jié)構(gòu)的下一級(jí)中查找?
感謝您提供的任何幫助或建議。
我在起草代碼時(shí)參考了這兩篇文章:
按兒童類別劃分的 WooCommerce 相關(guān)產(chǎn)品作為排名數(shù)學(xué)主要類別的后備
使用自定義分類法在 Woocommerce 中選擇相關(guān)產(chǎn)品
這是我當(dāng)前正在使用的代碼:
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íng)論):
// 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; }
它應(yīng)該可以工作。