WooCommerce:检查用户是否已经购买了产品

< 1 min read

如果您需要检测登录用户是否购买了某个产品ID,此代码段即可解决问题。您可以将其用于营销(例如,“购买更多!”)或在循环或单个产品页面上显示特殊通知。
仿牌独立站出海专家


add_action( 'woocommerce_after_shop_loop_item', 'bbloomer_user_logged_in_product_already_bought', 30 );
  
function bbloomer_user_logged_in_product_already_bought() {
global $product;
if ( ! is_user_logged_in() ) return;
$current_user = wp_get_current_user();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() ) ) echo '<div class="user-bought">&hearts; Hey ' . $current_user->first_name . ', you've purchased this in the past. Buy again?</div>';
}