检查客户是否在 Woocommerce 中为产品写了评论 [英] Check if customer wrote a review for a product in Woocommerce

查看:33
本文介绍了检查客户是否在 Woocommerce 中为产品写了评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码将检查客户是否已登录、他/她是否购买了产品以及这些陈述是否属实,它将显示一条消息.

我想在显示消息之前再添加一项检查,即客户是否已经对产品发表了评论/写了评论,如果是,请不要显示该消息.

换句话说,如果客户没有为产品写评论,则显示消息.如果客户留下评论,则不显示消息.

代码如下:

add_action('woocommerce_before_single_product_summary', 'woo_review_discount_message');函数 woo_review_discount_message() {如果 ( is_user_logged_in() ) {全球$产品;$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="用户购买"><span style="color:#CA364D;font-weight:bold;font-size:18px;"><i class="wishlist-icon icon-heart-o"><;/i>你好 ' .$current_user->first_name .'!请在下面写评论.

';}}

非常感谢您对此的任何帮助.

解决方案

要检查客户是否对当前产品发表了评论,您将使用此自定义条件函数(使用非常轻量级的 SQL 查询):

//用于检查客户是否在产品中发表评论的实用函数函数 has_reviewed_product( $product_id ) {全球 $wpdb;$user = wp_get_current_user();if( $user->ID == 0 )返回假;//统计商品数量$count = $wpdb->get_var("SELECT COUNT(comment_ID) FROM {$wpdb->prefix}commentsWHERE comment_post_ID = $product_idAND comment_author_email = '{$user->user_email}'");返回 $count >0 ?真假;}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

所以在你的代码中(来自 这个答案) 你将使用它如下:

//检查客户是否购买了产品的实用函数(仅限已完成"状态的订单)函数 customer_has_bought_product( $product_id, $user_id = 0 ) {全球 $wpdb;$customer_id = $user_id == 0 ||$user_id == '' ?get_current_user_id() : $user_id;$status = 'wc 完成';如果(!$customer_id)返回假;//统计商品数量$count = $wpdb->get_var("SELECT COUNT(woim.meta_value) FROM {$wpdb->prefix}posts AS pINNER JOIN {$wpdb->prefix}postmeta AS pm ON p.ID = pm.post_idINNER JOIN {$wpdb->prefix}woocommerce_order_items AS woi ON p.ID = woi.order_idINNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id哪里 p.post_status = '$status'AND pm.meta_key = '_customer_user'AND pm.meta_value = $customer_idAND woim.meta_key IN ('_product_id', '_variation_id')AND woim.meta_value = $product_id");//如果计数大于 0,则返回一个布尔值返回 $count >0 ?真假;}add_action('woocommerce_before_single_product_summary', 'woo_review_discount_message');函数 woo_review_discount_message() {全球$产品;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>你好 ' .$user->first_name .'!请在下面写评论.

';}}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

相关答案:如果用户已经在 Woocommerce 中购买了当前产品,则显示自定义文本

This code will check if the customer is logged in, if he/she purchased the product and if those statements are true, it will display a message.

I would like to include one more check before the message is shown and that is if the customer already left a review/ wrote a review for the product and if so, do not show the message.

In other words, if the customer did not write a review for the product, show message. If customer left a review, do not show message.

Here is the code:

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>';
}
}

Any help on this is very much appreciated.

解决方案

To check if a customer has posted a review on the current product, you will use this custom conditional function (using a very light SQL query):

// 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;
}

Code goes in function.php file of the active child theme (or active theme). Tested and works.

So in your code (from this answer) you will use it as following:

// 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>';
    }
}

Code goes in function.php file of the active child theme (or active theme). Tested and works.

Related answer: Display a custom text if user has already bought the current product In Woocommerce

这篇关于检查客户是否在 Woocommerce 中为产品写了评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
PHP最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆