
本教程旨在指导开发者如何在woocommerce中准确获取用户购买过的所有产品列表,包括同一产品被重复购买的情况。核心策略是摒弃直接查询产品,转而从用户订单入手,通过遍历用户的每一个订单及其包含的商品项,收集所有产品id。这种方法能够确保生成一个完整且包含重复购买记录的用户购买历史清单,为后续功能开发提供准确数据。
在WooCommerce开发中,有时需要展示一个用户购买过的所有产品列表。一个常见的误区是尝试直接通过 WP_Query 查询产品,然后使用 wc_customer_bought_product() 函数进行过滤。然而,这种方法通常只能判断用户是否购买过某个产品,而无法记录同一产品被购买多次(即重复购买)的情况,因为它只返回一个布尔值,指示“是”或“否”。当业务需求要求精确到每一次购买记录时,这种方法便显得力不从心。
为了准确获取包含重复购买在内的所有产品列表,最可靠的方法是基于用户的订单数据进行构建。WooCommerce将用户的每一次购买行为都记录在一个订单中,而每个订单又包含具体的商品项。通过遍历用户的订单及其商品项,我们可以精确地提取出所有购买过的产品ID,包括那些被重复购买的产品。
以下是实现上述逻辑的PHP代码示例:
<?php
// 确保在WordPress环境中运行,并且WooCommerce已激活
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/**
* 获取当前用户购买过的所有产品ID,包括重复购买项。
*
* @return array 包含所有产品ID的数组。
*/
function get_current_user_purchased_product_ids_with_duplicates() {
$current_user_id = get_current_user_id();
// 如果用户未登录,则返回空数组
if ( ! $current_user_id ) {
return [];
}
// 获取当前用户的所有订单
// 可以通过 'status' 参数过滤订单状态,例如只获取已完成的订单:
// 'status' => array('wc-completed', 'wc-processing')
$orders = wc_get_orders( array(
'customer_id' => $current_user_id,
'posts_per_page' => -1, // 获取所有订单
) );
$all_product_ids_nested = []; // 用于存储嵌套的产品ID数组
// 遍历用户的所有订单
foreach ( $orders as $order ) {
$order_product_ids = []; // 存储当前订单的产品ID
// 遍历订单中的每一个商品项
foreach ( $order->get_items() as $item_id => $item ) {
// 获取商品项对应的产品ID
$product_id = $item->get_product_id();
if ( $product_id ) {
$order_product_ids[] = $product_id;
}
}
// 将当前订单的产品ID数组添加到总的嵌套数组中
if ( ! empty( $order_product_ids ) ) {
$all_product_ids_nested[] = $order_product_ids;
}
}
// 扁平化数组,将所有产品ID合并到一个单一的数组中,保留重复项
// 使用 array_merge(...array_values($all_product_ids_nested)) 确保正确处理空数组
$product_ids = [];
if ( ! empty( $all_product_ids_nested ) ) {
$product_ids = array_merge( ...array_values( $all_product_ids_nested ) );
}
return $product_ids;
}
// 示例用法:
// $purchased_product_ids = get_current_user_purchased_product_ids_with_duplicates();
// if ( ! empty( $purchased_product_ids ) ) {
// echo '<p>您购买过的产品ID列表 (含重复项):</p>';
// echo '<ul>';
// foreach ( $purchased_product_ids as $product_id ) {
// echo '<li>产品ID: ' . esc_html( $product_id ) . ' - ' . get_the_title( $product_id ) . '</li>';
// }
// echo '</ul>';
// } else {
// echo '<p>您还没有购买任何产品。</p>';
// }
?>获取更多商品项数据: 除了产品ID,$item 对象还包含许多其他有用的信息,例如:
性能优化: 如果用户订单量非常大,直接获取所有订单可能会影响性能。在这种情况下,可以考虑:
用户界面展示: 获取到产品ID列表后,您可以使用这些ID通过 new WP_Query() 或 wc_get_product() 来获取产品对象,并将其展示在前端。
// 假设 $purchased_product_ids 已经获取到
if ( ! empty( $purchased_product_ids ) ) {
$args = array(
'post_type' => 'product',
'post__in' => $purchased_product_ids,
'posts_per_page' => -1, // 显示所有
'orderby' => 'post__in', // 保持原始ID顺序(可选)
);
$products_query = new WP_Query( $args );
if ( $products_query->have_posts() ) :
while ( $products_query->have_posts() ) : $products_query->the_post();
// 在这里显示产品信息,例如:
// wc_get_template_part( 'content', 'product' );
// 或自定义HTML
the_title( '<h3>', '</h3>' );
the_post_thumbnail();
echo '<p>' . get_the_excerpt() . '</p>';
endwhile;
wp_reset_postdata();
endif;
}请注意,直接使用 post__in 查询产品会去除重复项。如果需要展示重复购买的产品,则需要单独处理 $purchased_product_ids 数组,例如遍历该数组,然后对每个ID调用 wc_get_product() 并进行展示。
通过从用户订单而非直接从产品查询入手,我们可以构建一个准确反映用户所有购买记录(包括重复购买)的产品ID列表。这种方法利用了WooCommerce订单结构的完整性,为开发者提供了灵活且可靠的数据基础,以便于实现各种与用户购买历史相关的功能,如“我的购买”页面、推荐系统等。在实际应用中,应根据具体需求对订单状态和性能进行进一步的优化和调整。
以上就是WooCommerce:获取用户购买的所有产品列表(含重复购买项)的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号