Woocommerce: How to display number of products sold/downloaded? [closed]

I've been trying to find a programmatic way to count and display how many times a product has been sold or downloaded in woocommerce; The following snippet code is the solution offered by almost everyone, but it has ceased to be working anymore, if it did previously.

//Show Total Sales on Product Page //
add_action( 'woocommerce_single_product_summary', 'wp_product_sold_count', 11 );
function wp_product_sold_count() {
global $product;
$total_sold = get_post_meta( $product->id, 'total_sales', true );
if ( $total_sold ) echo '
' . sprintf( __( 'Total Sold: %s', 'woocommerce' ), $total_sold ) . '

';
}

Any idea how to modify the code? Thanks in advance.


Solution 1:

Use get_id() instead of id

//Show Total Sales on Product Page //

add_action( 'woocommerce_single_product_summary', 'wp_product_sold_count', 11 );
function wp_product_sold_count() {
 global $product;
 $total_sold = get_post_meta( $product->get_id(), 'total_sales', true );
 if ( $total_sold ) 
   echo '' . sprintf( __( 'Total Sold: %s', 'woocommerce' ), $total_sold ) . '';
}

Or you can use this plugin also: https://wordpress.org/plugins/wc-sales-count-manager/