1

I have custom attribute named "show_tab" in products. I have enable/disable value for this product. I want to show custom product tab according to attribute's value. Custom product tab is created successfully according to below way. But i don't know how to apply product specific condition in xml file?

To start with create a file ‘catalog_product_view.xml’ in the app/code///view/frontend/layout folder.

<?xml version="1.0"?>
<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="Magento\Catalog\Block\Product\View" name="custom.tab" template="VendorName_ModuleName::custom_tab.phtml" group="detailed_info" >
               <arguments>
                   <argument translate="true" name="title" xsi:type="string">Custom Tab</argument>
               </arguments>
           </block>
       </referenceBlock>
   </body>
</page>

Now created the custom_tab.phtml file in app/code///view/frontend/layout folder and added the below

<?php echo "This is Custom tab in product detail page"; ?>

How to apply condition in above code? Can anyone please help?

2 Answers 2

1

You just need to create custom block for it like below. And apply your condition like below.

<?php
namespace VendorName\ModuleName\Block\Product;

use Magento\Framework\View\Element\Template\Context;
use Magento\Framework\View\Element\Template;

class Tab extends \Magento\Framework\View\Element\Html\Link
{
    /**
     * @var \Magento\Framework\Registry
     */
    protected $_registry;

    /**
     * @param \Magento\Framework\View\Element\Template\Context $context
     * @param \Magento\Framework\Registry $registry
     * @param array $data
     */
    public function __construct(
        Context $context,
        \Magento\Framework\Registry $registry,
        array $data = []
    ) {
        $this->_helper = $helper;
        $this->_registry = $registry;
        parent::__construct($context, $data);
    }

    public function _toHtml() 
    {
        if($this->getProduct()) {
            $showTab = $this->getProduct()->getShowTab();
            if (!$showTab) {
                return '';
            }
        }
        return parent::_toHtml();
    }

    public function getProduct()
    {
        return $this->_registry->registry('current_product');
    }
}

Now call above custom block file in xml file indtead of block "Magento\Catalog\Block\Product\View"

<?xml version="1.0"?>
<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="VendorName\ModuleName\Block\Product\Tab" name="custom.tab" template="VendorName_ModuleName::custom_tab.phtml" group="detailed_info" >
               <arguments>
                   <argument translate="true" name="title" xsi:type="string">Custom Tab</argument>
               </arguments>
           </block>
       </referenceBlock>
   </body>
</page>
0

Try the below solution:

create events.xml in your module

[Vendor]/[Module]/etc/frontend/events.xml

    <?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
       <event name="layout_load_before">
        <observer name="load_custom_tab_handler" instance="[Vendor]\[Module]\Observer\LayoutLoadBefore" />
    </event>
</config>

In your [Vendor][Module]\Observer\LayoutLoadBefore.php file write below code

<?php

namespace [Vendor]\[Module]\Observer;

class LayoutLoadBefore implements \Magento\Framework\Event\ObserverInterface
{
    /**
     * @var \Magento\Framework\Registry
     */
    private $registry;

    public function __construct(
       ............
       \Magento\Framework\Registry $registry,
       ............
    ) {
        $this->registry = $registry;
    }


    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $product = $this->registry->registry('current_product');

        if (!$product) {
          return $this;
        }
        if (!$product->getshowTab()) { // your condition
           $layout = $observer->getLayout();
           $layout->getUpdate()->addHandle('catalog_product_view_customtab');
        }

        return $this;
    }
}

Cretae catalog_product_view_customtab.xml

[Vendor][Module]\view\frontend\layout\catalog_product_view_customtab.xml file write below code

<?xml version="1.0"?>
<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="custom.tab" remove="true" />
    </body>
    
</page>

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.