5

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?

1
  • 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 '18 at 12:20
7

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>';
}
3
  • 1
    Your code works. It only needs a closing to the echo: ...Download</a>'; – Punct Ulica Aug 27 '15 at 11:45
  • 6
    get_files() is now deprecated so users should use get_downloads(); you can update your comment – Uriahs Victor Mar 1 '18 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 ? – Umair Mehmood Jan 27 at 5:40

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