如何编写一个foreach循环来检查用户在Woocommerce中购买特定商品的次数?

如何编写一个foreach循环来检查用户在Woocommerce中购买特定商品的次数?,woocommerce,foreach,Woocommerce,Foreach,如果客户购买了产品13372,我将使用此代码生成一个iframe,并且效果非常好: <?php // Get the current user data: $user = wp_get_current_user(); $user_id = $user->ID; // Get the user ID $customer_email = $user->user_email; // Get the user email // OR // $customer_email = get_u

如果客户购买了产品13372,我将使用此代码生成一个iframe,并且效果非常好:

<?php
// Get the current user data:
$user = wp_get_current_user();
$user_id = $user->ID; // Get the user ID
$customer_email = $user->user_email; // Get the user email
// OR
// $customer_email = get_user_meta( $user->ID, 'billing_email', true ); // Get the user billing email

// The conditional function (example)
// IMPORTANT: $product_id argument need to be defined
$product_id = 13372;
if( wc_customer_bought_product( $customer_email, $user_id, $product_id ) ) {
    echo "You have additional listings!"; 
    //iFrame goes here 
} else {
    echo "You have no additional listings.";
}
?>

现在我需要修改它来检查用户购买产品ID 13372的次数,并输出那么多的iFrame。如果购买3次,则输出3个iFrame。我假设使用foreach循环,但我尝试的方法不起作用。我跟随这篇文章:


但是这个例子没有为我返回任何东西,不知道为什么

请尝试以下操作。我们正在遍历特定客户的每个已完成的店铺订单,并将购买产品的所有ID放入一个数组中。然后,我们在该数组中循环,查看特定ID出现的次数。我们将该值存储在
$count
变量中,该变量用于确定输出iframe的次数

我已经对代码进行了注释,并尽可能清楚地说明了函数和变量名