【问题标题】:Woocommerce 3.0 related products filteringWoocommerce 3.0 相关产品过滤
【发布时间】:2018-06-03 16:13:06
【问题描述】:

有没有办法从相关产品部分中排除具有特定值属性的产品。例如,我想要除具有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'
              )
        )
    );
}

编辑:我发现我可以使用过滤器 woocommerce_product_related_posts_query 编辑从数据库中获取相关产品的查询。

我尝试了以下代码:

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

但我收到错误报告:“where 子句”中的未知列“t.term_id”

【问题讨论】:

    标签: mysql wordpress woocommerce


    【解决方案1】:

    我设法通过使用过滤器woocommerce_product_related_posts_query 并像这样编辑 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' );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-12
      • 2021-06-10
      • 1970-01-01
      • 1970-01-01
      • 2016-02-16
      相关资源
      最近更新 更多