Woocommerce 中当前用户按产品的总购买次数 [英] Total purchase count by product for current user in Woocommerce

查看:63
本文介绍了Woocommerce 中当前用户按产品的总购买次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Woocommerce 中,我想在单个产品页面中显示当前用户的总购买产品数.例如,如果 John 买了一支笔 2 次,那么它会在这个产品页面中为 John 用户显示数量 ("2") ,如果 Jack 买了 5 次,那么它会在这个产品页面中显示 5杰克用户.

我不想打印总销售量,我想按当前登录用户显示.

我在 function.php 文件中的实际代码:

add_action( 'woocommerce_single_product_summary', 'wc_product_sold_count', 11 );函数 wc_product_sold_count() {$get_current_pro_id = $_SESSION["iddd"];全球$产品;$current_user = wp_get_current_user();if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID, $product->get_id() ) ){$units_sold = get_post_meta( $product->id, 'total_sales', true );//echo '

'.sprintf( __( 'Units Sold: %s', 'woocommerce' ), $units_sold ) .'</p>';返回 $units_sold;}}

解决方案

这可以通过在您的挂钩函数中使用非常轻量级的 SQL 查询轻松完成:

add_action( 'woocommerce_single_product_summary', 'wc_product_sold_count', 11 );函数 wc_product_sold_count() {//仅适用于登录用户如果(!is_user_logged_in())返回;//非登录用户退出全球 $wpdb, $product;$user_id = get_current_user_id();//当前用户 ID$product_id = $product->get_id();//当前产品 ID//SQL请求$units_bought = $wpdb->get_var( "选择总和(woim2.meta_value)FROM {$wpdb->prefix}woocommerce_order_items AS woiINNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta woim ON woi.order_item_id = woim.order_item_id内连接{$wpdb->前缀}woocommerce_order_itemmeta woim2 ON woi.order_item_id = woim2.order_item_idINNER JOIN {$wpdb->prefix}postmeta pm ON woi.order_id = pm.post_idINNER JOIN {$wpdb->prefix}posts AS p ON woi.order_id = p.IDWHERE woi.order_item_type LIKE 'line_item'AND p.post_type LIKE 'shop_order'AND p.post_status IN ('wc-completed','wc-processing')AND pm.meta_key = '_customer_user'AND pm.meta_value = '$user_id'AND woim.meta_key = '_product_id'AND woim.meta_value = '$product_id'AND woim2.meta_key = '_qty'");//如果大于零则显示计数如果( $units_bought > 0 ){$label = __( '单位购买' , 'woocommerce' );//标签//输出echo '<p class="units-bought"><strong>'.$标签.':</strong>'.$units_bought .'</p>';}}

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

相关:

In Woocommerce, I would like to show the total purchase product count for the current user in single product pages. Like for example if John bought a Pen 2 times then it displays the count ("2") in this product page for John user and if Jack bought it 5 times then it will show 5 in this product page for Jack user.

I don't want print total sold count, I want to show as per current logged in user.

My actual code in function.php file:

add_action( 'woocommerce_single_product_summary', 'wc_product_sold_count', 11 );
function wc_product_sold_count() {
    $get_current_pro_id = $_SESSION["iddd"];

    global $product;
    $current_user = wp_get_current_user();

    if ( wc_customer_bought_product( $current_user->user_email, $current_user->ID,  $product->get_id() )  )
    {

        $units_sold = get_post_meta( $product->id, 'total_sales', true );
//echo '<p>' . sprintf( __( 'Units Sold: %s', 'woocommerce' ), $units_sold ) . '</p>';

        return $units_sold;
    }
}

解决方案

That can be done easily with a very light SQL query in your hooked function:

add_action( 'woocommerce_single_product_summary', 'wc_product_sold_count', 11 );
function wc_product_sold_count() {
    // Only for logged in users
    if ( ! is_user_logged_in() ) return; // Exit for non logged users

    global $wpdb, $product;

    $user_id = get_current_user_id(); // Current User ID
    $product_id = $product->get_id(); // Current Product ID

    // The SQL request
    $units_bought = $wpdb->get_var( "
        SELECT SUM(woim2.meta_value)
        FROM {$wpdb->prefix}woocommerce_order_items AS woi
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta woim ON woi.order_item_id = woim.order_item_id
        INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta woim2 ON woi.order_item_id = woim2.order_item_id
        INNER JOIN {$wpdb->prefix}postmeta pm ON woi.order_id = pm.post_id
        INNER JOIN {$wpdb->prefix}posts AS p ON woi.order_id = p.ID
        WHERE woi.order_item_type LIKE 'line_item'
        AND p.post_type LIKE 'shop_order'
        AND p.post_status IN ('wc-completed','wc-processing')
        AND pm.meta_key = '_customer_user'
        AND pm.meta_value = '$user_id'
        AND woim.meta_key = '_product_id'
        AND woim.meta_value = '$product_id'
        AND woim2.meta_key = '_qty'
    ");
    
    // Display count if is greater than zero
    if( $units_bought > 0 ){
        $label = __( 'Units bought' , 'woocommerce' ); // Label
        
        // Output
        echo '<p class="units-bought"><strong>' . $label . ': </strong>' . $units_bought . '</p>';
    }
}

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

Related: Display the total purchase count of a specific product for customer in Woocommerce

这篇关于Woocommerce 中当前用户按产品的总购买次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆