如果用户以前购买过类似产品,如何在 woocommerce 结帐页面上显示自定义消息
How to Display a custom message on woocommerce checkout page if user previously purchased a similar product
在我的网站上,如果用户之前购买了他们将要购买的产品,我想在结帐页面上显示一条自定义消息。所以我遇到了这段代码,如果用户已经购买了产品,它实际上会显示自定义消息,该代码仅显示在单个产品页面上。请问如何在结帐页面上显示此消息?
add_action( 'woocommerce_before_add_to_cart_form', 'user_logged_in_product_already_bought', 30 );
function user_logged_in_product_already_bought() {
global $product;
if ( ! is_user_logged_in() ) return;
if ( wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {
echo '<div>You purchased this in the past. Buy again?</div>';
}
}
正如 @Dmitry
所建议的那样,您可以使用 woocommerce_before_checkout_form
操作挂钩。但您无法在结帐页面上访问 global $product;
。试试下面的代码。
function user_logged_in_product_already_bought() {
global $woocommerce;
if ( ! is_user_logged_in() ) return;
// check if current user in specific role.
$user = wp_get_current_user();
if ( in_array( 'customer', (array) $user->roles ) ) {
}
$items = $woocommerce->cart->get_cart();
$has_bought = false;
foreach($items as $item => $values) {
if ( has_bought_items( get_current_user_id(), $values['data']->get_id() ) ) {
$has_bought = true;
break;
}
}
if( $has_bought ){
wc_print_notice( "You purchased this in the past. Buy again?", 'success' );
}
}
add_action( 'woocommerce_before_checkout_form', 'user_logged_in_product_already_bought' );
已测试并有效
根据 @7uc1f3r
评论, I will like to prevent the double purchase of product per user.
您可以使用 woocommerce_add_to_cart_validation
动作挂钩以防止从购物车中添加产品。
function prevent_from_adding_cart_if_user_product_already_bought( $passed, $product_id, $quantity ) {
if ( has_bought_items( get_current_user_id(), $product_id ) ) {
wc_add_notice( __( "You purchased this in the past. Buy again?", 'woocommerce' ), 'error' );
$passed = false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'prevent_from_adding_cart_if_user_product_already_bought', 10, 3 );
已测试并有效
您可以使用 制作的这个很棒的功能来检查用户是否购买了特定产品。
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;
}
根据 OP 请求更新。
您可以使用下面的代码 check if the current user is in a specific role.
$user = wp_get_current_user();
if ( in_array( 'customer', (array) $user->roles ) ) {
}
并且只有在 has_bought_items
函数中处理状态为 'processing 的产品才会更改 $paid_statuses
变量的值。根据您的需要。
在我的网站上,如果用户之前购买了他们将要购买的产品,我想在结帐页面上显示一条自定义消息。所以我遇到了这段代码,如果用户已经购买了产品,它实际上会显示自定义消息,该代码仅显示在单个产品页面上。请问如何在结帐页面上显示此消息?
add_action( 'woocommerce_before_add_to_cart_form', 'user_logged_in_product_already_bought', 30 );
function user_logged_in_product_already_bought() {
global $product;
if ( ! is_user_logged_in() ) return;
if ( wc_customer_bought_product( '', get_current_user_id(), $product->get_id() ) ) {
echo '<div>You purchased this in the past. Buy again?</div>';
}
}
正如 @Dmitry
所建议的那样,您可以使用 woocommerce_before_checkout_form
操作挂钩。但您无法在结帐页面上访问 global $product;
。试试下面的代码。
function user_logged_in_product_already_bought() {
global $woocommerce;
if ( ! is_user_logged_in() ) return;
// check if current user in specific role.
$user = wp_get_current_user();
if ( in_array( 'customer', (array) $user->roles ) ) {
}
$items = $woocommerce->cart->get_cart();
$has_bought = false;
foreach($items as $item => $values) {
if ( has_bought_items( get_current_user_id(), $values['data']->get_id() ) ) {
$has_bought = true;
break;
}
}
if( $has_bought ){
wc_print_notice( "You purchased this in the past. Buy again?", 'success' );
}
}
add_action( 'woocommerce_before_checkout_form', 'user_logged_in_product_already_bought' );
已测试并有效
根据 @7uc1f3r
评论, I will like to prevent the double purchase of product per user.
您可以使用 woocommerce_add_to_cart_validation
动作挂钩以防止从购物车中添加产品。
function prevent_from_adding_cart_if_user_product_already_bought( $passed, $product_id, $quantity ) {
if ( has_bought_items( get_current_user_id(), $product_id ) ) {
wc_add_notice( __( "You purchased this in the past. Buy again?", 'woocommerce' ), 'error' );
$passed = false;
}
return $passed;
}
add_filter( 'woocommerce_add_to_cart_validation', 'prevent_from_adding_cart_if_user_product_already_bought', 10, 3 );
已测试并有效
您可以使用
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;
}
根据 OP 请求更新。
您可以使用下面的代码 check if the current user is in a specific role.
$user = wp_get_current_user();
if ( in_array( 'customer', (array) $user->roles ) ) {
}
并且只有在 has_bought_items
函数中处理状态为 'processing 的产品才会更改 $paid_statuses
变量的值。根据您的需要。