Php 根据特定的产品属性值显示与商业相关的产品

Php 根据特定的产品属性值显示与商业相关的产品,php,wordpress,woocommerce,custom-taxonomy,taxonomy-terms,Php,Wordpress,Woocommerce,Custom Taxonomy,Taxonomy Terms,我试图根据特定的产品属性“pa_kolekcja”术语值(在产品上设置)获取显示的相关产品。我有一段代码(它几乎准备好了): function-woo\u-related\u-products\u-edit(){ 全球$产品; $current_kolekcja=“???”;/“产品”, “忽略粘性帖子”=>1, “未找到行”=>1, “每页帖子数”=>4, 'orderby'=>$orderby, 'post__not_in'=>数组($product->id), “tax_query”=>数

我试图根据特定的产品属性“pa_kolekcja”术语值(在产品上设置)获取显示的相关产品。我有一段代码(它几乎准备好了):

function-woo\u-related\u-products\u-edit(){
全球$产品;
$current_kolekcja=“???”;/“产品”,
“忽略粘性帖子”=>1,
“未找到行”=>1,
“每页帖子数”=>4,
'orderby'=>$orderby,
'post__not_in'=>数组($product->id),
“tax_query”=>数组(
排列(
“分类法”=>pa_kolekcja',
'字段'=>'段塞',
“条款”=>$current\u kolekcja
)
)
) );
}
添加过滤器('woocommerce\u related\u products\u args'、'woo\u related\u products\u edit');

如何获取产品上设置的当前产品属性“pa_kolekcja”术语值?

更新

自woocommerce 3以来,
woocommerce\u相关产品\u args
已被删除

要显示当前产品中特定产品属性集的相关产品,请尝试以下操作:

add_filter( 'woocommerce_related_products', 'related_products_by_attribute', 10, 3 );
function related_products_by_attribute( $related_posts, $product_id, $args ) {
    $taxonomy   = 'pa_kolekcja'; // HERE define the targeted product attribute taxonomy

    $term_slugs = wp_get_post_terms( $product_id, $taxonomy, ['fields' => 'slugs'] ); // Get terms for the product

    if ( empty($term_slugs) )
        return $related_posts;

    $posts_ids = get_posts( array(
        'post_type'            => 'product',
        'ignore_sticky_posts'  => 1,
        'posts_per_page'       => 4,
        'post__not_in'         => array( $product_id ),
        'tax_query'            => array( array(
            'taxonomy' => $taxonomy,
            'field'    => 'slug',
            'terms'    => $term_slugs,
        ) ),
        'fields'  => 'ids',
        'orderby' => 'rand',
    ) );

    return count($posts_ids) > 0 ? $posts_ids : $related_posts;
}

代码进入活动子主题(或活动主题)的functions.php文件。测试和工作。

感谢LoicTheAztec,但它不会改变任何东西(产品仍像以前一样显示)。它是唯一可以控制相关产品的回路吗?或者如果我使用Elementor,Elementor可能有自己的回路?@Tim Oups!更新测试和工程。如果这个答案回答了你的问题,你可以取悦这个答案,如果你喜欢/想要,你也可以取悦这个答案,谢谢。这很有魅力。非常感谢。
add_filter( 'woocommerce_related_products', 'related_products_by_attribute', 10, 3 );
function related_products_by_attribute( $related_posts, $product_id, $args ) {
    $taxonomy   = 'pa_kolekcja'; // HERE define the targeted product attribute taxonomy

    $term_slugs = wp_get_post_terms( $product_id, $taxonomy, ['fields' => 'slugs'] ); // Get terms for the product

    if ( empty($term_slugs) )
        return $related_posts;

    $posts_ids = get_posts( array(
        'post_type'            => 'product',
        'ignore_sticky_posts'  => 1,
        'posts_per_page'       => 4,
        'post__not_in'         => array( $product_id ),
        'tax_query'            => array( array(
            'taxonomy' => $taxonomy,
            'field'    => 'slug',
            'terms'    => $term_slugs,
        ) ),
        'fields'  => 'ids',
        'orderby' => 'rand',
    ) );

    return count($posts_ids) > 0 ? $posts_ids : $related_posts;
}