If you want to set one more custom tab to show pdf & download them then follow the below steps.
Step 1: Create a file like app/code/Magemonkeys/Custompdftab/registration.php
1 2 3 4 5 6 7 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Magemonkeys_Custompdftab', __DIR__ ); ?> |
Step 2: Create a file like app/code/Magemonkeys/Custompdftab/etc/module.xml
1 2 3 4 5 6 7 |
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Magemonkeys_Custompdftab" setup_version="1.0.0"> <sequence> <module name="Magento_Catalog"/> </sequence> </module> </config> |
Step 3: Create a file like app/code/Magemonkeys/Custompdftab/Helper/Data.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
<?php namespace Magemonkeys\Custompdftab\Helper; use Magento\Framework\App\Helper\AbstractHelper; use Magento\Framework\App\Helper\Context; class Data extends AbstractHelper { public function stringToTable($text, $colSeparator = '|', $rowSeparator = '||') { $rows = explode($rowSeparator, $text); if (!$rows) return false; $html = '<table>'; foreach ($rows as $row) { if ($row) { $html .= '<tr>'; $columns = explode($colSeparator, $row); $w=count($columns); foreach ($columns as $column) { $html .= '<td style="width:'. 100/$w .'%">' . $column . '</td>'; } $html .= '</tr>'; } } $html .= '</table>'; return $html; } public function applicationsRender($applications, $separator = "||"){ //echo 'called';exit; if(!$applications) return; $html = ''; $_elements = ''; if(strpos($applications,':') > 0) { $_items = explode('.',$applications); natcasesort($_items); foreach($_items as $_item){ $_item = explode(':',$_item); if($_item[0] == '') continue; $html .= '<ul class="applications">'; $html .= trim($_item[0]).': '; $_elements = @explode($separator,$_item[1]); foreach ($_elements as $element){ $html .= '<li>'.trim($element).'</li>'; } $html .= '</ul>'; } } else { $html .= '<ul class="applications">'; $_items = explode($separator,$applications); natcasesort($_items); foreach ($_items as $_item){ $html .= '<li>'.$_item.'</li>'; } $html .= '</ul>'; } return $html; } } |
Step 4: Create a file like app/code/Magemonkeys/Custompdftab/view/frontend/layout/catalog_product_view.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?xml version="1.0"?> <page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"> <body> <referenceBlock name="product.info.details"> <block class="Magento\Catalog\Block\Product\View" name="custom-tab" template="Magemonkeys_Custompdftab::product/view/details/custom_tab.phtml" group="detailed_info"> <arguments> <argument name="css_class" xsi:type="string">newtabclass</argument> <argument translate="true" name="title" xsi:type="string">Documentatie</argument> <argument name="sort_order" xsi:type="string">60</argument> </arguments> </block> </referenceBlock> </body> </page> |
Step 5 : Create file like app/code/Magemonkeys/Custompdftab/view/frontend/templates/product/view/details/custom_tab.phtml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
<?php $_product = $block->getProduct(); $sku = $_product->getSku(); $file = $_product->getAgreementFile(); $objectManager = \Magento\Framework\App\ObjectManager::getInstance(); $fileSystem = $objectManager->create('\Magento\Framework\Filesystem'); $mediaPath = $fileSystem->getDirectoryRead(\Magento\Framework\App\Filesystem\DirectoryList::MEDIA)->getAbsolutePath(); $dir = '/'.$mediaPath.'/'.'catalog/product/file/'.$file; $dir2 = '/'.$mediaPath.'/'.'file/'.$sku; $flag = FALSE; $flag1 = true; ?> <?php if(file_exists($dir) && $file != ''):{ $flag1 = false; if($file){ $flag = TRUE; } ?> <a class="download-file" href="<?php echo $block->getBaseUrl(); ?>pub/media/catalog/product/file/<?php echo basename($file) ?>" target="_blank"><?php echo str_replace('.pdf', '', $file) ?></a> <?php } elseif(file_exists($dir2) && $flag1):{ if ($dh = opendir($dir2)){ while (($file = readdir($dh)) !== false){ if ($file != '.' && $file != '..') { if($file){ $flag = TRUE; }?> <a class="download-file1" href="<?php echo $block->getBaseUrl(); ?>pub/media/file/<?php echo $sku.'/'.basename($file) ?>" target="_blank"><?php echo str_replace('.pdf', '', $file) ?></a> <?php } } } } ?> <?php else: ?> <?php echo "There is no file"; ?> <?php endif; ?> <?php if($flag == TRUE): ?> <script> require(['jquery', 'jquery/ui'], function($){ $('#tab-label-custom-tab').css('display','block'); }); </script> <?php endif; ?> |
That’s it…
Now, you can check your product detail page. Your custom tab will be showing after all default tabs.
If you want get query string params in controller file,...
Create di.xml and add the below code Magemonkey/Redirect/etc/frontend/di.xml [crayon-62838c8822b86698832631/] Create...
You can try below code to change local date to...
Step 1: First you need to add registration.php file in...
Step1 : Override message.js in current theme file on the...