
本教程旨在详细指导如何在woocommerce产品页面上展示来自非当前产品所属类别的商品,以实现高级的交叉销售功能。文章将介绍两种策略:首先是排除直接关联的商品类别,其次是更复杂的、排除整个类别树(包括父类、子类及兄弟类)的实现方法,并提供相应的代码示例和最佳实践。
在电子商务运营中,智能地推荐相关商品是提升用户体验和销售额的关键策略之一。WooCommerce提供了多种内置的推荐机制,但有时我们需要更精细的控制,例如在某个商品页面上展示与当前商品完全不属于同一类别层级的商品,以实现更具创新性的交叉销售(Cross-selling)。本文将深入探讨如何通过自定义查询来实现这一目标,并提供两种不同粒度的类别排除策略。
我们的目标是:当用户访问一个特定商品(例如属于 cat_1_1 类别)的页面时,系统能够推荐来自其他不相关类别的商品。这意味着我们需要排除以下情况的商品:
这要求我们首先识别当前商品的所属类别,然后构建一个数据库查询,明确排除这些类别下的商品。
最直接的实现方式是获取当前商品所关联的所有类别ID,然后在查询其他商品时,将这些ID排除在外。这种方法适用于只需要排除当前商品直接所属类别的情况。
首先,在WooCommerce产品页面上下文中,我们需要获取当前商品的ID,并进而获取其所有关联的商品类别。
global $post; // 确保在WooCommerce产品循环或相关钩子中使用
$product_id = $post->ID; // 获取当前产品ID
$excluded_category_ids = array(); // 用于存储需要排除的类别ID
// 获取当前产品的所有商品类别
$terms = get_the_terms( $product_id, 'product_cat' );
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
$excluded_category_ids[] = $term->term_id; // 将类别ID添加到排除列表中
}
}获取到需要排除的类别ID数组后,我们可以使用 WP_Query 结合 tax_query 参数来构建查询,实现类别排除。
系统特点:功能简洁实用。目前互联网上最简洁的企业网站建设系统!原创程序代码。非网络一般下载后修改的代码。更安全。速度快!界面模版分离。原创的分离思路,完全不同于其他方式,不一样的简单感受!搜索引擎优化。做了基础的seo优化。对搜索引擎更友好系统功能关于我们:介绍企业介绍类信息,可自由添加多个介绍栏目!资讯中心:公司或行业资讯类内容展示。可自由添加多个资讯内容!产品展示:支持类别设置,可添加产品图片
0
// 构建WP_Query参数
$args = array(
'post_type' => 'product', // 查询商品类型
'posts_per_page' => 5, // 显示5个商品
'post__not_in' => array( $product_id ), // 排除当前正在查看的商品本身
'tax_query' => array( // 税分类查询
array(
'taxonomy' => 'product_cat', // 针对商品类别分类法
'field' => 'term_id', // 使用term_id进行匹配
'terms' => $excluded_category_ids, // 需要排除的类别ID数组
'operator' => 'NOT IN' // 运算符:不包含在指定ID中
),
),
'orderby' => 'rand', // 随机排序,增加推荐的多样性
);
// 执行查询
$cross_sell_products = new WP_Query( $args );
// 检查是否有符合条件的商品
if ( $cross_sell_products->have_posts() ) :
echo '<h2>您可能还喜欢</h2>';
echo '<ul class="cross-sell-products-list">';
while ( $cross_sell_products->have_posts() ) : $cross_sell_products->the_post();
// 在这里输出商品的HTML结构,例如:
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
<h3><?php the_title(); ?></h3>
<?php woocommerce_template_loop_price(); ?>
</a>
</li>
<?php
endwhile;
echo '</ul>';
endif;
// 重置查询,恢复主循环的数据
wp_reset_postdata();如果需求是排除当前商品所属类别 及其所有父类、子类和兄弟类,那么仅仅排除直接关联的类别是不够的。我们需要更复杂的逻辑来识别整个类别分支。
例如,如果产品属于 cat_1_1,我们可能希望排除 cat_1、cat_1_1 和 cat_1_2 下的所有产品。这要求我们首先找到当前类别的顶层父类别,然后获取该顶层父类别下的所有子类别(包括其自身)。
global $post;
$product_id = $post->ID;
$excluded_category_ids = array();
// 获取当前产品的所有直接关联类别
$terms = get_the_terms( $product_id, 'product_cat' );
if ( $terms && ! is_wp_error( $terms ) ) {
foreach ( $terms as $term ) {
// 1. 将当前类别ID添加到排除列表
$excluded_category_ids[] = $term->term_id;
// 2. 找到当前类别的所有祖先类别
$ancestors = get_ancestors( $term->term_id, 'product_cat', 'taxonomy' );
// 3. 确定顶层父类别ID (如果没有祖先,则当前类别就是顶层)
$top_level_parent_id = empty( $ancestors ) ? $term->term_id : end( $ancestors );
// 4. 将顶层父类别本身添加到排除列表 (如果尚未添加)
if ( ! in_array( $top_level_parent_id, $excluded_category_ids ) ) {
$excluded_category_ids[] = $top_level_parent_id;
}
// 5. 获取顶层父类别下的所有子类别 (包括所有层级)
$all_descendants = get_terms( array(
'taxonomy' => 'product_cat',
'child_of' => $top_level_parent_id,
'fields' => 'ids',
'hide_empty' => false, // 即使类别下没有商品也获取其ID
) );
// 6. 将所有子类别ID合并到排除列表
$excluded_category_ids = array_merge( $excluded_category_ids, $all_descendants );
}
}
// 确保排除列表中没有重复的ID
$excluded_category_ids = array_unique( $excluded_category_ids );
// 此时,$excluded_category_ids 包含了当前产品所属类别及其整个父分支下的所有类别ID。收集到所有需要排除的类别ID后,查询方法与策略一类似,只是 terms 数组包含了更广泛的ID。
// 构建WP_Query参数
$args = array(
'post_type' => 'product',
'posts_per_page' => 5,
'post__not_in' => array( $product_id ), // 排除当前商品
'tax_query' => array(
array(
'taxonomy' => 'product_cat',
'field' => 'term_id',
'terms' => $excluded_category_ids, // 使用包含整个分支的排除列表
'operator' => 'NOT IN'
),
),
'orderby' => 'rand',
);
// 执行查询和展示 (与策略一相同)
$cross_sell_products = new WP_Query( $args );
if ( $cross_sell_products->have_posts() ) :
echo '<h2>您可能还喜欢</h2>';
echo '<ul class="cross-sell-products-list">';
while ( $cross_sell_products->have_posts() ) : $cross_sell_products->the_post();
?>
<li>
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( 'thumbnail' ); ?>
<h3><?php the_title(); ?></h3>
<?php woocommerce_template_loop_price(); ?>
</a>
</li>
<?php
endwhile;
echo '</ul>';
endif;
wp_reset_postdata();通过本文介绍的两种策略,您可以灵活地在WooCommerce产品页面上实现基于类别排除的交叉销售推荐。无论是简单的排除直接关联类别,还是复杂的排除整个类别层级分支,核心都是利用 get_the_terms() 获取当前商品的类别信息,并结合 WP_Query 的 tax_query 和 NOT IN 运算符来精确控制推荐商品的来源。合理运用这些技术,将有助于您构建更智能、更有效的商品推荐系统,从而提升店铺的销售表现。
以上就是WooCommerce产品页面的跨类别商品展示教程的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号