The other day I got an interesting support thread for Easy Digital Downloads, asking for the list of files, and their sizes be displayed when viewing a product. Being that I only use it to sell WordPress plugins, it seemed pretty minor, but if you were dealing with larger Audio/Video files, or your consumers used their mobile devices frequently, I could see how this would be useful. So I put together a quick, single file, plugin that is now available in the EDD Library Repo on Github.

First, here’s the primary function that is run:

function edd_ck_show_file_sizes( $post_id ) {
	$files = edd_get_download_files( $post_id, null );
	$decimals = 2;
	$sz = 'BKMGTP';
	$header = _n( 'File Size', 'File Sizes', count( $files ), 'edd' );
	echo '<h5>' . $header . '</h5>';
	echo '<ul>';
	foreach( $files as $file ) {
		$bytes = filesize( get_attached_file( $file['attachment_id'] ) );
		$factor = floor((strlen($bytes) - 1) / 3);
		echo '<li>' . $file['name'] . ' - ' . sprintf( "%.{$decimals}f", $bytes / pow( 1024, $factor) ) . @$sz[$factor] . '</li>';
	}
	echo '</ul>';
}
add_action( 'edd_after_download_content', 'edd_ck_show_file_sizes', 10, 1 );

Now, a quick overview. Basically what we’re doing is looking for all the uploaded media items attached as ‘Downloads’ to this product. Once we get them, we’re iterating through them. You see this in the line

$files = edd_get_download_files( $post_id, null );

After that we do some setup, only wanting 2 decimal places, setting up the title of the list (following the numeric localization method), and you’ll notice that odd variable BKMGTP. What is this? Well it’s used to make the output of the size calculation human readable. It’s for “Bytes, Kilobytes, Megabytes, Gigabytes, Terabytes, and Petabytes”. I’ve never seen a use case of someone selling Tera/Petabyte files on EDD, but who am I to judge.

After that, a little loop to run each calculation by powers of 1024, and we’re good to go!

You can go grab the full plugin-ready file here.

Image credit(CC): John Trainor

Posted by Chris Klosowski

Chris Klosowski is the Director of Technology at Sandhills Development, LLC, creators of fine WordPress plugins, and ecommerce solutions.

15 Comments

  1. […] EDD? Here’s a very handy snippet to show file sizes for downloads on the product pages. Sumobi also released a free extension for add-to-cart redirects on […]

    Reply

    1. This is a great suggestion, thanks!

      Reply

  2. Hi Chris, thanks for the code! I’ve added this to functions.php and then on my product page I’ve added;

    $post = get_post();
    edd_ck_show_file_sizes( $post->ID );

    which is almost working. Strangely, if I upload a file with the fes submission form it shows the size as 0.00, but if I upload a file from the WordPress dashboard it seems to show the size correctly. Any idea why this might be? Screenshot; https://i.imgur.com/E67VUal.png

    Thanks again.

    Tom

    Reply

    1. Hi, I am facing the same problem. As I found out, this is all from the fact that the plugin FES does not add parameter ‘attachment_id’ to the table ‘wp_postmeta’ in line ‘edd_download_files’.
      I found the file plugins/edd-fes/classes/fields/multiple_pricing.php
      and in the lines 513 and 523 added: ‘attachment_id’ => fes_get_attachment_id_from_url( $url, $user_id )
      And it all worked)
      But I don’t know if all this is right and there will be no problems in the future …) Thanks

      Reply

  3. hi, how to show
    file name with extension ?
    ex:
    name : test size : 39 kb type: zip

    tnx

    Reply

  4. There’s definately a great deal to know about this issue.
    I love all of the points you have made.

    Reply

  5. I’m really impressed together with your writing abilities and also with the structure in your weblog.
    Is that this a paid topic or did you customize it yourself?
    Anyway keep up the nice high quality writing, it’s rare to look a nice weblog like this one nowadays.

    my site; mpc-install.com

    Reply

  6. Today, I went to the beach front with my kids. I found a sea
    shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She
    placed the shell to her ear and screamed. There
    was a hermit crab inside and it pinched her ear. She never
    wants to go back! LoL I know this is entirely off topic but I had to tell
    someone!

    Reply

  7. Cytotec (Misoprostol) is used for reducing the risk of stomach ulcers in certain patients who take nonsteroidal anti-inflammatory drugs (NSAIDs).
    Buy Cytotec (Misoprostol) right now – https://artevinostudio.com/myphp/buyamoxilonline/ and save your money.
    Only 1.85$ Per pill!

    Reply

  8. That is really fascinating, You’re an overly skilled blogger.
    I have joined your rss feed and stay up for in the hunt for more of your fantastic post.
    Also, I have shared your web site in my social networks!

    My webpage … http://www.gazaldesign.xyz

    Reply

  9. Thanks for sharing your thoughts about code. Regards

    Reply

  10. Amoxicillin is a penicillin antibiotic. It kills or stops the growth of some bacteria. This medicine is used to treat many kinds of infections. It will not work for colds, flu, or other viral infections.
    Buy Amoxicillin right now – http://amoxicillina.weebly.com/ and save your money.
    Only 0.44 usd per pill!

    Reply

Leave a reply

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