WooCommerce3.0相关产品过滤

q1qsirdb  于 2021-06-20  发布在  Mysql
关注(0)|答案(1)|浏览(389)

是否有一种方法可以将具有特定值属性的产品从相关产品部分排除。例如,我希望相关的产品按类别,除了那些与属性 attribute_value('pa_season') = summer; 我试过了,但没用

add_filter( 'woocommerce_related_products_args', 'filter_related_products' );
function filter_related_products( $args ) {
    global $product;
    return array(
       'post_type'            => 'product',
       'ignore_sticky_posts'  => 1,
       'no_found_rows'        => 1,
       'posts_per_page'       => 4,
       'orderby'              => 'ASC',
       'post__not_in'         => array( $product->get_id() ),
       'tax_query'      =>     array(
              array(
                  'taxonomy' => 'pa_epoxi',
                  'field' => 'slug',
                  'terms' => 'autumn-winter'
              )
        )
    );
}

编辑:我发现我可以使用filter-woocmerce\u-product\u-related\u-posts\u查询来编辑从数据库中获取相关产品的查询。
我尝试了以下代码:

add_filter( 'woocommerce_product_related_posts_query', function( $query ) {
    $query['where'] .= " AND t.term_id !=100";
    return $query;
});

但我得到一个错误报告:“where子句”中的未知列“t.term\u id”

mec1mxoz

mec1mxoz1#

我通过使用filter\u product\u related\u posts\u查询并按如下方式编辑sql查询,成功地排除了所需的术语ID:

function exclude_terms_from_related_products( $query ) {
    global $wpdb;
    $exclude_term_ids[0] = 100; // term_id
    $query['join']  .= " LEFT JOIN ( SELECT object_id FROM {$wpdb->term_relationships} WHERE term_taxonomy_id IN ( " . implode( ',', array_map( 'absint', $exclude_term_ids ) ) . ' ) ) AS exclude_join1 ON exclude_join1.object_id = p.ID';
    $query['where'] .= ' AND exclude_join1.object_id IS NULL';
    return $query;
}
add_filter( 'woocommerce_product_related_posts_query', 'exclude_terms_from_related_products' );

相关问题