php 在WooCommerce中显示客户未审查的购买产品

aiqt4smr  于 2023-06-28  发布在  PHP
关注(0)|答案(1)|浏览(145)

我会很感激你的帮助。我有一个代码,在[customer_products]的短代码中显示当前用户购买的产品。
我需要检查短代码的内容,看看用户是否在他们购买的产品上留下了评论。
1.如果用户对购买的产品留下评论,则该产品不显示在短代码中。
1.如果用户没有为购买的产品留下评论,则显示短代码中的产品(没有评论的产品),并显示文本。
请帮助,我是一个PHP初学者,我明白我需要has_reviewed_product函数,因为它在另一个答案中使用:Check if customer wrote a review for a product in Woocommerce
但是我不明白如何在短代码的输出代码中写这个检查。
非常感谢大家的帮助。

add_shortcode( 'customer_products', 'rrr_products_current_user' );
 
function rrr_products_current_user() {
    
    if ( ! is_user_logged_in() ) {
        return;
    }
 
    
    $customer_orders = get_posts( array(
        'posts_per_page' => -1,
        'meta_key'    => '_customer_user',
        'meta_value'  => get_current_user_id(),
        'post_type'   => wc_get_order_types(),
        'post_status' => array_keys( wc_get_is_paid_statuses() ),
    ) );
 
    
    if ( ! $customer_orders ) {
        return;
    }
 
    
    $ids = array();
    foreach ( $customer_orders as $customer_order ) {
        $order = wc_get_order( $customer_order->ID );
        $items = $order->get_items();
        foreach ( $items as $item ) {
            $ids[] = $item->get_product_id();
        }
    }
 
    
    return do_shortcode( '[products ids="' . join( ",", array_unique( $ids ) ) . '"]' );
 
}
bgibtngc

bgibtngc1#

最好使用wc_get_order()函数,因为WooCommerce订单很快就会在WordPress数据库中拥有自己的表。
下面的简码只会为客户显示未审核的购买产品,假设在WooCommerce设置>产品>评论中,您已经启用了“评论只能由“验证所有者”留下”选项:

下面是代码:

// Utility function that get the reviewved products Ids of a customer
function get_customer_reviewed_products( $user_email ) {
    global $wpdb;

    // Returns an array of product Ids reviewed by the customer
    return $wpdb->get_col( "
        SELECT DISTINCT c.comment_post_ID 
        FROM {$wpdb->prefix}comments as c
        INNER JOIN {$wpdb->prefix}posts as p ON p.ID = c.comment_post_ID 
        WHERE c.comment_author_email = '{$user_email}'
            AND p.post_type = 'product'
    " );
}

// The custom products shortcode
add_shortcode( 'customer_products', 'customer_products_shortcode' );
function customer_products_shortcode() {
    if ( ! is_user_logged_in() ) {
        return;
    }

    $user = wp_get_current_user(); // GEt WP_User  object
    
    // Get customer orders
    $customer_orders = wc_get_orders( array(
        'limit'         => -1,
        'customer'      => $user->ID,
        'status'        => wc_get_is_paid_statuses(),
    ) );

    $product_ids = array(); // Initializing

    // Loop through customer orders
    foreach ( $customer_orders as $customer_order ) {
        // Loop through order items
        foreach ( $customer_order->get_items() as $item ) {
            $product_ids[] = $item->get_product_id();
        }
    }
 
    if ( empty($customer_orders) ){
        return;
    }

    // Get customer reviewed products ids
    $reviewed_ids = get_customer_reviewed_products( $user->user_email );
    
    // Keep only customer unreviewed purchased products
    $ids = implode( ',', array_diff( array_unique( $product_ids ), $reviewed_ids ) );
    
    return do_shortcode( "[products ids='$ids']" );
}

代码放在你的活动子主题(或活动主题)的function.php文件中。测试和作品。

用法:

在WordPress文本编辑器、小部件或块中:

[customer_products]

在PHP中:

echo do_shortcode("[customer_products]");

相关问题