I'm trying to add the direct download link of a product to a button, but I'm stuck.

What I tried is this

<a href="<?php echo $product->downloadable_files; ?>">Download</a>

And of course it didn't work.

What is the proper code to get the product's download url?

  • Note that you should never output the actual downloadable file to a user, but instead grant the user permissions on a downloadable, and output that permissions link. Like this, a user can share a link to a downloadable that does not require authentication.
    – kontur
    Dec 10, 2018 at 12:20

1 Answer 1


Here is the code to get the download link for a downloadable product in WooCommerce.

$downloads = $product->get_downloads();

foreach( $downloads as $key => $each_download ) {
  echo '<a href="'.$each_download["file"].'">Download</a>';
  • 1
    Your code works. It only needs a closing to the echo: ...Download</a>'; Aug 27, 2015 at 11:45
  • 6
    get_files() is now deprecated so users should use get_downloads(); you can update your comment Mar 1, 2018 at 18:58
  • hi can you please guide me lit bit where i need to write this code? in theme functions.php or in any other file ? Jan 27, 2021 at 5:40

Not the answer you're looking for? Browse other questions tagged or ask your own question.