pathosmusic
pathosmusic

Reputation: 43

Finding src for WooCommerce downloadable product files to display in Audio Shortcode

What I'm trying to do is add the default Wordpress audio shortcode to the content-single-product.php template from WooCommerce. I understand that the default shortcode is supposed to find any attached audio if there is none specified, but it's not behaving as such. I guess WooCommerce downloadable products handle file attachments differently?

This is the code I am using so far"

$ca_audio = $product->get_id();
$attr = array(
	'src'      => $ca_audio,
	'loop'     => '',
	'autoplay' => '',
	'preload'  => 'none'
);
echo wp_audio_shortcode( $attr );

This is just displaying the product ID instead of a player. If i leave src blank it shows an empty player with no audio. If i put a direct link to the file it will play, but I need it to auto find the .mp3 file for each post automatically.

Upvotes: 0

Views: 324

Answers (1)

Manik Malhotra
Manik Malhotra

Reputation: 632

I used the following code to show audio file

$ca_audio = $product->get_id();

$_product = wc_get_product( $ca_audio );
$downloads = $product->get_files();

foreach( $downloads as $key => $each_download ) {
  	
	if ( strpos( $each_download["file"] , '.mp3') !== false) {
	    $attr = array(
			'src'      => $each_download["file"],
			'loop'     => '',
			'autoplay' => '',
			'preload'  => 'none'
		);
		echo wp_audio_shortcode( $attr );
		break;
	}
}

The audio file is correctly embedded but it is not playing. When I open the file directly, it gives 401 Forbidden error. It is a woocommerce downloadable product access security feature. Maybe you need to manually embed audio files in each product.

Upvotes: 0

Related Questions