Total Download Count Woocommerce

add_action( 'woocommerce_single_product_summary', 'show_number_of_downloads' );
function show_number_of_downloads() {
    global $wpdb, $product;
    if ( empty( $product->id ) ) return;
    if ( $product->product_type == 'variable' ) {
        $product_ids = $product->get_children();
    } else {
        $product_ids = array( $product->id );
    }
    $query = "SELECT SUM( download_count ) AS count
                    FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
                    WHERE product_id IN (".implode( ',', $product_ids ).")";
    $count = $wpdb->get_var( $query );
    if ( ! empty( $count ) ) {
        echo '<p><strong>' . __( 'Total downloads' ) . '</strong>: ' . $count . '</p>';
    }
}
See also  How to Change WooCommerce SKU text to another label

Leave a Reply

Your email address will not be published. Required fields are marked *