2

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>
1

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>
0

Replace your layout xml :

   <referenceContainer name="content">
        <referenceBlock name="product.info.details">
            <block class="Test\CustomCart\Block\CustomTab" name="mycustom.tab"" template="Test_CustomCart::kit.phtml" group="detailed_info">
                <arguments>
                    <argument translate="true" name="title" xsi:type="string">Custom Tab</argument>
                </arguments>
            </block>
        </referenceBlock>
    </referenceContainer>

After that please remove/flush cache and reindex then check.

  • where is the logic? is it enough? – Bojjaiah Nov 28 '16 at 10:21
  • yes try this. It works – Ronak Chauhan Nov 28 '16 at 10:22
0

To do this you can use Attribute set.

In your product edit page, set the custom tab attribute only on those products in which you wants to display Customer tab on product detail page.

You can also check my answer on "My Custom product tab not displaying in Magento 2".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.