Magento 2 – How to Show/Hide Custom Tab in Product Page

catalogmagento-2.1producttabs

I have created a custom tab and which is working and showing fine on the product page. Here how can I show/hide the custom tab based on product type (simple, bundle etc…).

my code is

Block

<?php
namespace Test\CustomCart\Block;

use Magento\Catalog\Model\Product;

class CustomTab extends \Magento\Framework\View\Element\Template{

    /**
     * @var Product
     */
    protected $_product = null;

    /**
     * Core registry
     *
     * @var \Magento\Framework\Registry
     */
    protected $_coreRegistry = null;

    /**
     * @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
     */
    protected $productRepository;

    /**
     * @var \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
     */
    protected $dataObjectHelper;


    /**
     * @var \Magento\Catalog\Model\ProductFactory
     */
    protected $_productFactory;


    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Catalog\Api\ProductRepositoryInterface $productRepository
     * @param \Magento\Framework\Api\DataObjectHelper $dataObjectHelper
     * @param \Magento\Catalog\Model\ProductFactory $productFactory
     * @param \Magento\Framework\Registry $registry
     * @param array $data
     */
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
        \Magento\Framework\Api\DataObjectHelper $dataObjectHelper,
        \Magento\Catalog\Model\ProductFactory $productFactory,
        \Magento\Framework\Registry $registry,
        array $data = []
    ) {
        $this->_coreRegistry = $registry;
        $this->productRepository = $productRepository;
        $this->dataObjectHelper = $dataObjectHelper;
        $this->_productFactory = $productFactory;
        parent::__construct($context, $data);
    }

    /**
     * @return Product
     */
    public function getProduct()
    {
        if (!$this->_product) {
            $this->_product = $this->_coreRegistry->registry('product');
        }
        return $this->_product;
    }


}

view/frontend/layout/catalog_product_view.xml

<?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="Test\CustomCart\Block\CustomTab" name="mycustom.tab" as="mycustom" template="Test_CustomCart::kit.phtml" group="detailed_info">
                <arguments>
                    <argument name="title" translate="true" xsi:type="string">Custom Tab</argument>
                    <argument name="sortOrder" xsi:type="string">0</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

/view/frontend/templates/kit.phtml

<h1>Hey I'm here!!!</h1>

Best Answer

If you want to show tab in simple product create catalog_product_view_type_simple.xml file add your code in it.

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->
<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="Test\CustomCart\Block\CustomTab" name="mycustom.tab" as="mycustom" template="Test_CustomCart::kit.phtml" group="detailed_info">
                <arguments>
                    <argument name="title" translate="true" xsi:type="string">Custom Tab</argument>
                    <argument name="sortOrder" xsi:type="string">0</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

If you want to display tab in bundle product add code in catalog_product_view_type_bundle.xml

<?xml version="1.0"?>
    <!--
    /**
     * Copyright © 2016 Magento. All rights reserved.
     * See COPYING.txt for license details.
     */
    -->
    <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="Test\CustomCart\Block\CustomTab" name="mycustom.tab" as="mycustom" template="Test_CustomCart::kit.phtml" group="detailed_info">
                    <arguments>
                        <argument name="title" translate="true" xsi:type="string">Custom Tab</argument>
                        <argument name="sortOrder" xsi:type="string">0</argument>
                    </arguments>
                </block>
            </referenceBlock>
        </body>
    </page>
Related Topic