在woocommerce中,有时我们希望在特定的商品展示页面(例如主商店页)只显示某种特定类型的产品,如组合商品(grouped products)。一种常见的实现方式是使用woocommerce_product_query_tax_query过滤器来修改商品查询的分类条件。然而,这种全局性的修改可能会带来意想不到的副作用。
例如,当我们在functions.php中添加以下代码,旨在让商店页只显示组合商品时:
/** * Show Only Grouped Products in Woocommerce */ add_filter( 'woocommerce_product_query_tax_query', 'only_grouped_products', 20, 1 ); function only_grouped_products( $tax_query ){ $tax_query[] = array( 'taxonomy' => 'product_type', 'field' => 'name', 'terms' => array('grouped'), ); return $tax_query; }
这段代码确实能使WooCommerce商店页只显示组合商品。但是,它会影响整个网站的所有商品查询,包括由第三方插件(如WooCommerce Product Vendors)生成的供应商页面。这意味着,供应商页面上原本应该显示的独立商品(Simple Products)或其他类型商品将无法显示,因为全局过滤器强制它们也必须是“组合商品”类型,从而导致页面内容缺失。
为了解决这个问题,我们需要引入条件逻辑,确保商品查询的修改只在特定页面生效。
WooCommerce提供了一个强大的动作钩子 woocommerce_product_query,它在商品查询执行之前触发,允许我们直接访问和修改WP_Query对象。结合WordPress的条件标签(Conditional Tags),我们可以精确地控制何时应用商品类型过滤。
以下是优化后的解决方案代码,应放置在主题的functions.php文件或自定义插件中:
/** * WooCommerce特定页面商品查询优化:仅显示组合商品 * 确保在指定页面(如商店页、特定页面、分类页、标签页)显示组合商品, * 同时不影响其他页面(如供应商页面)的商品显示。 */ add_action( 'woocommerce_product_query', 'prefix_custom_pre_get_posts_query' ); function prefix_custom_pre_get_posts_query( $q ) { // 检查当前页面是否为商店页或特定页面(例如“products-portfolio”) if( is_shop() || is_page('products-portfolio') ) { $tax_query = (array) $q->get( 'tax_query' ); // 获取现有分类查询条件 $tax_query[] = array( 'taxonomy' => 'product_type', // 商品类型分类法 'field' => 'name', // 查询字段为分类法的名称 'terms' => array( 'grouped'), // 仅查询“组合商品”类型 ); $q->set( 'tax_query', $tax_query ); // 设置修改后的分类查询条件 } // 检查当前页面是否为商品分类归档页 elseif( is_product_category() ) { $tax_query = (array) $q->get( 'tax_query' ); $tax_query[] = array( 'taxonomy' => 'product_type', 'field' => 'name', 'terms' => array( 'grouped'), ); $q->set( 'tax_query', $tax_query ); } // 检查当前页面是否为商品标签归档页 elseif( is_product_tag() ) { $tax_query = (array) $q->get( 'tax_query' ); $tax_query[] = array( 'taxonomy' => 'product_type', 'field' => 'name', 'terms' => array( 'grouped'), ); $q->set( 'tax_query', $tax_query ); } // 对于其他所有页面(包括供应商页面),不进行任何修改,保持默认商品查询行为 else { // 无需操作 } }
add_action( 'woocommerce_product_query', ... ):
条件标签 (is_shop(), is_page(), is_product_category(), is_product_tag()):
修改 tax_query:
else 块:
通过上述方法,我们成功解决了WooCommerce特定页面仅显示组合商品与第三方插件(如Product Vendors)供应商页面商品显示冲突的问题。核心思想在于利用woocommerce_product_query动作钩子和WordPress条件标签,实现对商品查询的精细化控制。这种有条件的应用查询修改,不仅能满足特定页面的展示需求,还能避免对网站其他功能产生不必要的负面影响,提升了网站的整体兼容性和用户体验。在实际应用中,请务必在开发或测试环境中进行充分测试,以确保代码的正确性和稳定性。
以上就是WooCommerce特定页面商品显示优化:实现组合商品与供应商页面兼容展示的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 //m.sbmmt.com/ All Rights Reserved | php.cn | 湘ICP备2023035733号