默认情况下使用产品属性值过滤 Woocommerce 存档页面

Filter Woocommerce archive pages with product attribute values by default

我希望产品存档页面默认显示与特定属性值对应的产品。

例子
属性:pa_condition
条款:全新、二手、清仓

我希望开店时只看到新产品。

但是有两种语言。所以我想,条件应该适用于属性 id。

如何做到这一点?

更新: 你可以试试这个钩子函数:

add_filter( 'woocommerce_product_query_tax_query', 'custom_product_query_tax_query', 10, 2 );
function custom_product_query_tax_query( $tax_query, $query ) {
    if( is_admin() ) return $tax_query;

    // Define HERE the product attribute and the terms
    $taxonomy = 'pa_condition';
    $terms = array( 'New', 'Used', 'Closeout' ); // Term names

    // Add your criteria
    $tax_query[] = array(
        'taxonomy' => $taxonomy,
        'field'    => 'name', // Or 'slug' or 'term_id'
        'terms'    => $terms,
    );
    return $tax_query;
}

代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。

已测试并有效。

但我不确定这些术语的翻译,因此您必须将它们添加到查询该翻译术语的第二个 $tax_query[] 数组中……


官方参考:WP_Query ~ Taxonomy Parameters