Posted on

How to Show Download Count on WooCommerce Product Page?

This article mentions a custom code you can add to your 'functions.php' file to show the download count of digital products on your WooCommerce product page.

Last updated on July 5, 2021

Show download count on WooCommerce Product Page of your store. It will contribute to your sales pitch. And as the download count will grow, your visitor will get confidence on your product. They will know that other peoples are using your product as well.

If you’re in business of selling digital / downloadable products, you’ll need good number of downloads to increase your sale. Download counts certainly affects purchase behaviour of your visitor. When they see a positive download count, they may consider purchasing your product. These numbers will always keep growing. And it’ll add a great point in pitching your product.

How the download count will look

Download count on simple product page in WooCommerce
Download count on variable product page in WooCommerce

How to enable download count on WooCommerce product page

  1. Choose a file which will never be updated during any kind of update, whether it’s WordPress upgrade, plugin updates or theme update. Most commonly it can either be functions.php file of your custom theme or any other file of your custom plugin.
  2. Add following code in that file
    function show_number_of_downloads() {
        global $wpdb, $product;
    
        $product_id = ( is_object( $product ) && is_callable( array( $product, 'get_id' ) ) ) ? $product->get_id() : 0;
    
        if ( empty( $product_id ) ) return;
    
        $product_type = ( is_object( $product ) && is_callable( array( $product, 'get_type' ) ) ) ? $product->get_type() : 'simple';
    
        if ( 'variable' === $product_type ) {
            $product_ids = $product->get_children();
        } else {
            $product_ids = array( $product_id );
        }
    
        $how_many_product_ids = count( $product_ids );
        $id_placeholder       = array_fill( 0, $how_many_product_ids, '%d' );
    
        $count = $wpdb->get_var(
            $wpdb->prepare(
                "SELECT SUM( download_count ) AS count
                    FROM {$wpdb->prefix}woocommerce_downloadable_product_permissions
                    WHERE product_id IN (".implode( ',', $id_placeholder ).")",
                $product_ids
            )
        );
        if ( ! empty( $count ) ) {
            echo '<strong>' . esc_html__( 'Total downloads' ) . '</strong>: ' . $count;
        }
    }
    add_action( 'woocommerce_single_product_summary', 'show_number_of_downloads' );
    

    Note: This code will work with WooCommerce 2.5.0+

  3. Save the file. You’re done!

The above code will work for both simple & variable products. For simple products, it’ll simply show total download counts. But a variable product can have more than a variations & each variations can provide either separate or same downloadable file.

Therefore download count of each variations are recorded separately. The above code automatically combines download count of each variations & display on product page.

Learn how to setup digital / downloadable product in WooCommerce.

8 thoughts on “How to Show Download Count on WooCommerce Product Page?

  1. Awesome tutorial. Thank you.
    How can I show total product views on WooCommerce product page like this? With a little “eye” icon? Can you please help me?

    1. Thank you for your appreciation.

      Here’s one free plugin on WordPress Product View Counter Page Visit Counter

  2. Product View Counter is no longer compatible. Can anyone suggest an alternate option?

    1. You can consider Page Visit Counter

  3. Hi,

    Thank you for the snippets, it works great.
    One question, would be how to control the position of where it appears ?
    For now it shows under the price – But I’d like to have it show up under the add to cart button.
    Can you help with the position ?

    Let me know
    thanks!

    1. In the above-mentioned code, replace this line of code
      add_action( 'woocommerce_single_product_summary', 'show_number_of_downloads' );
      with this
      add_action( 'woocommerce_after_add_to_cart_button', 'show_number_of_downloads' );

  4. Thank you for the snippets, it works great.
    i want to add total download for all product example in footer
    Thank you

    1. The above-mentioned snippet is only for a single product. It’s not possible to do a smaller tweaking in the above-mentioned code to display total downloads for all products. It will require an entirely new snippet.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.