Jump to content

Magento 2 Product PDF Download (Instructions etc)


Jules72

Recommended Posts

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 } ?>

Link to comment
Share on other sites

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>
            ";
     } 
 } 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.