检查客户是否在 Woocommerce 中为产品写了评论
Check if customer wrote a review for a product in Woocommerce
此代码将检查客户是否已登录,是否 he/she 购买了产品,如果这些陈述属实,它将显示一条消息。
我想在显示消息之前再进行一项检查,即如果客户已经为产品留下了评论/写了评论,那么不要显示消息。
换句话说,如果客户没有为产品写评论,则显示消息。如果客户留下评论,则不显示消息。
代码如下:
add_action( 'woocommerce_before_single_product_summary', 'woo_review_discount_message');
function woo_review_discount_message() {
if ( is_user_logged_in() ) {
global $product;
$current_user = wp_get_current_user();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() && $order->status->complete ) ) echo '<div class="user-bought"><span style="color:#CA364D;font-weight:bold;font-size:18px;"><i class="wishlist-icon icon-heart-o"></i></span> Hi ' . $current_user->first_name . '! Please write a review below.</a></div>';
}
}
非常感谢任何帮助。
要检查客户是否对当前产品发表了评论,您将使用此自定义条件函数(使用非常简单的 SQL 查询):
// Utility function to check if a customer has posted a review in a product
function has_reviewed_product( $product_id ) {
global $wpdb;
$user = wp_get_current_user();
if( $user->ID == 0 )
return false;
// Count the number of products
$count = $wpdb->get_var( "
SELECT COUNT(comment_ID) FROM {$wpdb->prefix}comments
WHERE comment_post_ID = $product_id
AND comment_author_email = '{$user->user_email}'
" );
return $count > 0 ? true : false;
}
代码进入活动子主题(或活动主题)的 function.php 文件。已测试并有效。
因此在您的代码中 (来自 ) 您将按如下方式使用它:
// Utility function to check if a customer has bought a product (Order with "completed" status only)
function customer_has_bought_product( $product_id, $user_id = 0 ) {
global $wpdb;
$customer_id = $user_id == 0 || $user_id == '' ? get_current_user_id() : $user_id;
$status = 'wc-completed';
if( ! $customer_id )
return false;
// Count the number of products
$count = $wpdb->get_var( "
SELECT COUNT(woim.meta_value) 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 = '$status'
AND pm.meta_key = '_customer_user'
AND pm.meta_value = $customer_id
AND woim.meta_key IN ( '_product_id', '_variation_id' )
AND woim.meta_value = $product_id
" );
// Return a boolean value if count is higher than 0
return $count > 0 ? true : false;
}
add_action( 'woocommerce_before_single_product_summary', 'woo_review_discount_message');
function woo_review_discount_message() {
global $product;
if ( customer_has_bought_product( $product->get_id() ) && ! $product->is_type('variable') && ! has_reviewed_product( $product->get_id() ) ) {
$user = wp_get_current_user();
echo '<div class="user-bought"><span style="color:#CA364D;font-weight:bold;font-size:18px;"><i class="wishlist-icon icon-heart-o"></i></span> Hi ' . $user->first_name . '! Please write a review below.</a></div>';
}
}
代码进入活动子主题(或活动主题)的 function.php 文件。已测试并有效。
相关回答:
此代码将检查客户是否已登录,是否 he/she 购买了产品,如果这些陈述属实,它将显示一条消息。
我想在显示消息之前再进行一项检查,即如果客户已经为产品留下了评论/写了评论,那么不要显示消息。
换句话说,如果客户没有为产品写评论,则显示消息。如果客户留下评论,则不显示消息。
代码如下:
add_action( 'woocommerce_before_single_product_summary', 'woo_review_discount_message');
function woo_review_discount_message() {
if ( is_user_logged_in() ) {
global $product;
$current_user = wp_get_current_user();
if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() && $order->status->complete ) ) echo '<div class="user-bought"><span style="color:#CA364D;font-weight:bold;font-size:18px;"><i class="wishlist-icon icon-heart-o"></i></span> Hi ' . $current_user->first_name . '! Please write a review below.</a></div>';
}
}
非常感谢任何帮助。
要检查客户是否对当前产品发表了评论,您将使用此自定义条件函数(使用非常简单的 SQL 查询):
// Utility function to check if a customer has posted a review in a product
function has_reviewed_product( $product_id ) {
global $wpdb;
$user = wp_get_current_user();
if( $user->ID == 0 )
return false;
// Count the number of products
$count = $wpdb->get_var( "
SELECT COUNT(comment_ID) FROM {$wpdb->prefix}comments
WHERE comment_post_ID = $product_id
AND comment_author_email = '{$user->user_email}'
" );
return $count > 0 ? true : false;
}
代码进入活动子主题(或活动主题)的 function.php 文件。已测试并有效。
因此在您的代码中 (来自
// Utility function to check if a customer has bought a product (Order with "completed" status only)
function customer_has_bought_product( $product_id, $user_id = 0 ) {
global $wpdb;
$customer_id = $user_id == 0 || $user_id == '' ? get_current_user_id() : $user_id;
$status = 'wc-completed';
if( ! $customer_id )
return false;
// Count the number of products
$count = $wpdb->get_var( "
SELECT COUNT(woim.meta_value) 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 = '$status'
AND pm.meta_key = '_customer_user'
AND pm.meta_value = $customer_id
AND woim.meta_key IN ( '_product_id', '_variation_id' )
AND woim.meta_value = $product_id
" );
// Return a boolean value if count is higher than 0
return $count > 0 ? true : false;
}
add_action( 'woocommerce_before_single_product_summary', 'woo_review_discount_message');
function woo_review_discount_message() {
global $product;
if ( customer_has_bought_product( $product->get_id() ) && ! $product->is_type('variable') && ! has_reviewed_product( $product->get_id() ) ) {
$user = wp_get_current_user();
echo '<div class="user-bought"><span style="color:#CA364D;font-weight:bold;font-size:18px;"><i class="wishlist-icon icon-heart-o"></i></span> Hi ' . $user->first_name . '! Please write a review below.</a></div>';
}
}
代码进入活动子主题(或活动主题)的 function.php 文件。已测试并有效。
相关回答: