Jules72 Posted May 24, 2023 Share Posted May 24, 2023 Code below is used on a Magento 2 website to display a download link for product instructions etc. We have some products that require more than one PDF, we can enter these is the Download attribute (dz_downloads) separated by a comma, but only one displays? If anyone could help amend the code so that we can display more than one PDF download per product it would be appriciated. <?php $product = $block->getProduct(); $blockObj = $block->getLayout()->createBlock('Dzinehub\Core\Block\Main'); $mediaUrl = $blockObj->getMediaUrl().'pdf_upload/'; $BaseUrl = $blockObj->getBaseUrl(); $mediapath = $blockObj->getMediaDir(); $pdfOption = 'dz_downloads'; $pdfoptionvalue = $product->getData($pdfOption); $pdf_files = explode(',', $pdfoptionvalue); foreach ($pdf_files as $pdf_file) { $pdfURL = $mediaUrl."".$pdf_file; $pdfPath = $mediapath .'/pdf_upload/' .$pdf_file; } ?> <?php if($pdf_file != "" && file_exists($pdfPath)){ ?> <div class="dz_pdf"> <a href="<?php echo $pdfURL; ?>" download><?php echo $pdf_file; ?></a> </div> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/316344-magento-2-product-pdf-download-instructions-etc/ Share on other sites More sharing options...
Barand Posted May 24, 2023 Share Posted May 24, 2023 Output the link/s in the foreach loop foreach ($pdf_files as $pdf_file) { $pdfURL = $mediaUrl."".$pdf_file; $pdfPath = $mediapath .'/pdf_upload/' .$pdf_file; if($pdf_file != "" && file_exists($pdfPath)) echo "<div class='dz_pdf'> <a href='$pdfURL' download>$pdf_file</a> </div> "; } } Quote Link to comment https://forums.phpfreaks.com/topic/316344-magento-2-product-pdf-download-instructions-etc/#findComment-1608630 Share on other sites More sharing options...
Jules72 Posted May 24, 2023 Author Share Posted May 24, 2023 14 minutes ago, Barand said: Output the link/s in the foreach loop foreach ($pdf_files as $pdf_file) { $pdfURL = $mediaUrl."".$pdf_file; $pdfPath = $mediapath .'/pdf_upload/' .$pdf_file; if($pdf_file != "" && file_exists($pdfPath)) echo "<div class='dz_pdf'> <a href='$pdfURL' download>$pdf_file</a> </div> "; } } Thanks, could you explain how to do it? Happy to make a donation! The file names are "XXX-XXXX.pdf" separated by a comma Quote Link to comment https://forums.phpfreaks.com/topic/316344-magento-2-product-pdf-download-instructions-etc/#findComment-1608631 Share on other sites More sharing options...
gizmola Posted May 27, 2023 Share Posted May 27, 2023 Barand already did that. He changed the code as required, to make it work. Did you try it to see if it worked? Quote Link to comment https://forums.phpfreaks.com/topic/316344-magento-2-product-pdf-download-instructions-etc/#findComment-1608686 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.