In this blog we’ll see how to add Custom tab add layout in product detail page in Magento 2.
we will create Custom module and follow below steps to add Custom tab on product detail page.
Step 1: Create register.php
app/code/Bluethinkinc/PdpCustomTab/registration.php
1 2 3 4 5 6 |
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Bluethinkinc_PdpCustomTab', __DIR__ ); |
Step 2: Create module.xml
app/code/Bluethinkinc/PdpCustomTab/etc/module.xml
1 2 3 4 5 |
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"> <module name="Bluethinkinc_PdpCustomTab" setup_version="1.0.0"> </module> </config> |
Step 3: Create Index.php
app/code/Bluethinkinc/PdpCustomTab/Block/Index/Index.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 |
<?php /** * Copyright © BluethinkInc All rights reserved. * See COPYING.txt for license details. */ namespace Bluethinkinc\PdpCustomTab\Block\Index; use Magento\Framework\Registry; use Magento\Framework\View\Element\Template; use Magento\Framework\View\Element\Template\Context; use Magento\Store\Model\StoreManagerInterface; class Index extends Template { /** * @var Registry */ protected $registry; /** * @var StoreManagerInterface */ protected $storeManager; /** * @param StoreManagerInterface $storeManager * @param Registry $registry * @param Context $context * @param array $data */ public function __construct( StoreManagerInterface $storeManager, Registry $registry, Context $context, array $data = [] ) { $this->storeManager = $storeManager; $this->registry = $registry; parent::__construct($context, $data); } /** * Get Section Title */ public function getSectionTitle() { $data='Custom Tab'; return $data; } } ?> |
Step 4: Create catalog_product_view.xml
app/code/Bluethinkinc/PdpCustomTab/view/frontend/layout/catalog_product_view.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0"?> <!-- /** * Copyright © BluethinkInc All rights reserved. * See COPYING.txt for license details. */ --> <page layout="1column" 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="Bluethinkinc\PdpCustomTab\Block\Index\Index" name="catalog.product.list.bluethinkinc.pdpcustomtab" as="bluethinkinc_product_pdpcustomtab" template="Bluethinkinc_PdpCustomTab::customtab.phtml" group="detailed_info" cacheable="false"> <arguments> <argument name="title" xsi:type="string">Custom Tab</argument> <argument name="sort_order" xsi:type="string">50</argument> </arguments> </block> </referenceBlock> </body> </page> |
Step 5: Create customtab.phtml
app/code/Bluethinkinc/PdpCustomTab/view/frontend/templates/customtab.phtml
1 2 3 4 5 6 7 8 9 10 11 12 |
<div class="questionanswertab"> <?php /** * Copyright © BluethinkInc All rights reserved. * See COPYING.txt for license details. */ /** @var \Magento\Framework\Escaper $escaper */ /** @var \Bluethinkinc\PdpCustomTab\Block\Index\Index $block */ ?> <h1>My New Tab</h1> </div> |
Step 6: After create the module if you run the command as
php bin/magento module:status
You should see the module is disable now:
List of disabled modules: Bluethinkinc_PdpCustomTab
enable the module right now, let run the command as:
php bin/magento module:enable Bluethinkinc_PdpCustomTab
After Enable Module Then Run this Command
sudo php bin/magento setup:upgrade
sudo php bin/magento setup:di:compile
sudo php bin/magento setup:static-content:deploy -f
sudo php bin/magento c:f
sudo chmod -R 777 var/ pub/static generated/
Now, please see the result
Mukesh Singh
2024-01-31