如果您需要检测登录用户是否购买了某个产品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">♥ Hey ' . $current_user->first_name . ', you've purchased this in the past. Buy again?</div>';
}