You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改代码仅在Imperial Adapters、Metric Adapters分类展示grouped产品

解决方案

你可以通过新增指定商品分类的判断逻辑,仅在匹配到对应分类时才添加grouped产品筛选规则,修改后可直接使用的完整代码如下:

function only_grouped_products_for_specific_categories( $tax_query, $query ) {
    // 此处填入你要生效的商品分类标识,支持填分类ID、别名、名称,下方判断会自动适配
    $target_categories = array( 'Imperial Adapters', 'Metric Adapters' );
    
    // 仅在指定的商品分类页应用筛选规则
    if ( is_product_category( $target_categories ) ) {
        $tax_query[] = array(
            'taxonomy'  => 'product_type',
            'field'     => 'name',
            'terms'     => array('grouped'),
        );
    }
    return $tax_query;
}
// 挂载规则到WooCommerce商品查询钩子
add_filter( 'woocommerce_product_query_tax_query', 'only_grouped_products_for_specific_categories', 10, 2 );

注意事项

  • 上面代码$target_categories数组内默认传的是分类名称,如果你用分类ID或者别名,直接替换数组内容即可,is_product_category函数会自动识别
  • 该规则仅对「Imperial Adapters」和「Metric Adapters」两个商品分类的列表页生效,其余分类、首页、搜索页等所有其他页面的商品展示不受影响

内容的提问来源于stack exchange,提问作者Yucaree

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 03:51:00