2020 年更新: 现在处理来自帐单电子邮件的访客用户。
新版本紧凑、轻巧、速度更快且兼容所有版本的 woocommerce(从 2.4 及更高版本开始)
这是一个新的增强和更轻的条件函数,部分基于内置的 woocommerce 函数wc_customer_bought_product 源代码。
有 2 个可选参数:
- 指定定义的用户 ID(当不用于当前登录用户时)
- 或来宾用户的帐单电子邮件;
-
$product_ids (array) 将允许指定一个或多个要检查的产品 ID
代码如下:
function has_bought_items( $user_var = 0, $product_ids = 0 ) {
global $wpdb;
// Based on user ID (registered users)
if ( is_numeric( $user_var) ) {
$meta_key = '_customer_user';
$meta_value = $user_var == 0 ? (int) get_current_user_id() : (int) $user_var;
}
// Based on billing email (Guest users)
else {
$meta_key = '_billing_email';
$meta_value = sanitize_email( $user_var );
}
$paid_statuses = array_map( 'esc_sql', wc_get_is_paid_statuses() );
$product_ids = is_array( $product_ids ) ? implode(',', $product_ids) : $product_ids;
$line_meta_value = $product_ids != ( 0 || '' ) ? 'AND woim.meta_value IN ('.$product_ids.')' : 'AND woim.meta_value != 0';
// Count the number of products
$count = $wpdb->get_var( "
SELECT COUNT(p.ID) FROM {$wpdb->prefix}posts AS p
INNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_id
INNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
WHERE p.post_status IN ( 'wc-" . implode( "','wc-", $paid_statuses ) . "' )
AND pm.meta_key = '$meta_key'
AND pm.meta_value = '$meta_value'
AND woim.meta_key IN ( '_product_id', '_variation_id' ) $line_meta_value
" );
// Return true if count is higher than 0 (or false)
return $count > 0 ? true : false;
}
代码进入您的活动子主题(或主题)的functions.php 文件或任何插件文件中。
此代码在 WooCommerce 3+ 上经过测试并且可以正常工作(它应该也可以在以前的版本上工作)。
使用示例:
示例 1 (登录客户):检测当前用户是否购买了定义的产品之一(产品 Ids 需要是一个数组) em>
// Define the targeted Products IDs
$product_ids = array( 38, 41, 85, 95 );
if( has_bought_items( '', $product_ids ) )
echo "<p>You have already purchased one of this products</p>";
else
echo "<p>You have not yet purchased one of this products</p>";
示例 2 (对于定义的 用户 id) 检测定义的用户是否购买了定义的产品之一 (产品ids需要在数组中设置)
// Define the user ID
$user_id = 85;
// Define the targeted Products IDs
$product_ids = array( 38, 41, 85, 95 );
if( has_bought_items( $user_id, $product_ids ) )
echo "<p>This user have already purchased one of this products</p>";
else
echo "<p>This user have not yet purchased one of this products</p>";
如果$user_id未定义且当前用户未登录,此函数将返回false。
示例 3 (针对 guest 用户) 检测 guest 用户 是否从以下网站购买了定义的产品之一他的帐单电子邮件 (需要在数组中设置产品ID)
// Define guest Billing email (string)
$user_email = 'louis.fourteen@gmail.com';
// Define the targeted Products IDs
$product_ids = array( 38, 41, 85, 95 );
if( has_bought_items( $user_email, $product_ids ) )
echo "<p>This user have already purchased one of this products</p>";
else
echo "<p>This user have not yet purchased one of this products</p>";
如果$user_id未定义且当前用户未登录,此函数将返回false。
示例 4 (已登录客户):检测当前用户是否已经购买
if( has_bought_items() )
echo '<p>You have already made a purchase</p>';
else
echo '<p>Welcome, for your first purchase you will get a discount of 10%</p>';
示例 5 (定义 用户 id) - 检测定义的用户是否已经购买
// Define the user ID
$user_id = 85;
if( has_bought_items( $user_id ) )
echo '<p>customer have already made a purchase</p>';
else
echo '<p>Customer with 0 purshases</p>';
现在,如果用户 id 等于 0 且当前用户未登录,此函数将返回 false(如果未为访客用户定义计费电子邮件)。