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:
Code goes in function.php file of your active child theme (or active theme). Tested and works.