Mobile app version of vmapp.org
Login or Join
Pierce454

: Getting the filename associated with a sample in the "Downloadable Information" area in Magento I'm working on a customization of the "Samples" block on Products. Basically what I need to know

@Pierce454

Posted in: #Magento #Php

I'm working on a customization of the "Samples" block on Products. Basically what I need to know is how to get the file name from a sample.

So, I can use the following code in my view to get an array of samples:

<?php $_samples = $this->getSamples() ?>


... and I can iterate over those samples well enough. What I'm trying to figure out, is, how can I get the name of the file associated with that sample? I basically want to do a customization to the view where, if the sample file is an MP3, I display an embedded player. For all other file types, the default behavior (a link is shown, which when clicked, opens a new window and displays the sample) is perfectly fine.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Pierce454

1 Comments

Sorted by latest first Latest Oldest Best

 

@Harper822

So, if you concatenate $samplePath and $sampleFile together, you get the full path the the sample file, on disk. i.e.: $samplePath . $sampleFile gets you /path/to/samples/folder/sub-folder/path/to/file.mp3

In this example, it's actually not necessary to get the $samplePath, since you can just test whether or not $sampleFile ends with ".mp3", as shown below, but I wanted to demonstrate how to get to the full path anyhow.

<?php
if ($this->hasSamples()):
$_samples = $this->getSamples();

foreach ($_samples as $_sample):
$samplePath = $_sample->getBasePath(); // returns "/path/to/samples/folder"
$sampleFile = $_sample->getSampleFile(); // returns "/sub-folder/path/to/file.mp3"

// Figure out if the filename ends with ".mp3"
$_is_mp3 = (bool)(".mp3" == substr($sampleFile, -4));
if ($_is_mp3):
//show the mp3 player
else:
//show a link to the file
endif;

endforeach;

endif;
?>

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme