1

I offer downloadable products with woocommerce and each product is allowed to have multiple files.

I would like to display the file types/formats on the product page so users know what each product contains. Please see the image below.

enter image description here

One idea I have thought of is to get the names of the attached files, and then somehow print only the last four characters of each one. But I cannot figure out how to write a function that would make it work.

3

Place the following code in your meta.php template file immediately after the lines where it prints the categories and tags. Please note, the standard practice is that you shall copy the meta.php template file to your theme folder. So to override meta.php, copy: woocommerce/templates/single-product/meta.php from plugin folder to yourtheme/woocommerce/templates/single-product/meta.php

<?php

global $product;

$downloads = $product->get_files();

foreach( $downloads as $key => $each_download ) {
  $info     = pathinfo($each_download["file"]);
  $ext      = $info['extension'];
  $formats .= $ext . ", ";
}

echo '<p> Formats: '. $formats .'</p>';

?>
| improve this answer | |
  • Thank You. This is much better than what i had thought it would look like. I have accepted this answer since it is exactly what i asked for, but I would like assistance with something I didn't think would be a problem. How do I wrap a <code><span class="test"></span></code> around each format? – Stanley Umeanozie Jan 2 '16 at 17:46
  • 1
    To achieve that, change the $formats code line to $formats .= '<code><span class="test">' . $ext . '</span></code>, '; – Prasad Nevase Jan 3 '16 at 5:19
-1

How do that too for the File Size? I have a Woo for Digital Products.. But anyone codes not worked for OutPut File Size.. But File format work good.

The code in this thread works great! But only for the file extension, I asked if there is the same code for outputting the size of the file added to the Woo product (as a virtual, downloadable)? I try use all codes from forums where is WP filesize (file size output) but not worked nothing

UPDATE

that work but needed past ID = 8677 (Manual ..) how do that, get a attached file id (Auto to per Product)?

| improve this answer | |
  • 1
    This should be a question related to the specific answer, not an answer on it's own. – davemac May 22 at 23:50
  • This is not work ( wordpress.stackexchange.com/questions/477/… ) for me.. I add code to PHP Product output but nothing.. The code in this thread works great! But only for the file extension, I asked if there is the same code for outputting the size of the file added to the Woo product (as a virtual, downloadable)? I try use all codes from forums where is WP filesize (file size output) but not worked nothing – Serhio Ramus May 23 at 14:44
  • <?php $attach_id = 8677; $meta = wp_get_attachment_metadata( $attach_id ); if ( isset( $meta['filesize'] ) ) $file_size = $meta['filesize']; elseif ( $file = get_attached_file( $attach_id ) ) if( file_exists( $file ) ) $file_size = filesize( $file ); echo size_format( $file_size ); ?> that work but needed past ID = 8677 (Manual ..) how do that, get a attached file id (Auto to per Product)? – Serhio Ramus May 23 at 17:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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