4

I have created a product attribute like, display_custom_tab, if it is yes then show custom tab on product detail page, otherwise do not show tab.

can anyone please tell me how to write condition in xml or any other way to check condition.

Note : ifconfig will not work here, because product attributes are not stored in 'core_config_data'.

So, I have attributes like, enter image description here

and tab like this,enter image description here

but tab is always showing on page. I want that if attribute is set to off, tab should hide.

This is my xml to show custom tab.

   <referenceBlock name="product.info.details">
        <block class="Magento\Catalog\Block\Product\View"
               name="warrantyInfo.tab"
               template="Product_CustomTab::custom_tab.phtml"
               group="detailed_info">
               <!--ifconfig="product/is_warranty_display"-->
            <arguments>
                <argument name="sort_order" xsi:type="string">40</argument>
                <argument translate="true" name="title" xsi:type="string">Warranty Info</argument>
            </arguments>
        </block>
    </referenceBlock>
  • which kind of tab it is? – 26vivek Dec 25 '19 at 6:57
  • Its a custom tab on product detail page just like details, more information, review and then my_custom_tab – Dimple Panchal Dec 25 '19 at 7:00
  • Have you created a custom tab? If yes then please share the code. – 26vivek Dec 25 '19 at 7:18
  • It is not possible only with xml. You need to override details.phtml I think. Can you please share your custom tab xml? – Ranganathan Dec 25 '19 at 7:31
  • Code uploaded. can u please check and let me know it's working for you or not – Naresh Rupareliya Dec 25 '19 at 8:22
4

Please make your code same as below code

In this example i have warranty_display attribute code for Warranty Display and warranty_info attribute code for Warranty Info.

Your catalog_product_view.xml code should be

<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="warrantyInfo.tab"
                   template="Product_CustomTab::custom_tab.phtml"
                   group="detailed_info">
                   <!--ifconfig="product/is_warranty_display"-->
                <arguments>
                    <argument name="sort_order" xsi:type="string">40</argument>
                    <argument translate="true" name="title" xsi:type="string">Warranty Info</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

your custom_tab.phtml file code should be

<?php if($this->getProduct()->getWarrantyDisplay()): ?>
    <?php echo $this->getProduct()->getWarrantyInfo(); ?>
<?php endif; ?>

When attribute enabled:

enter image description here

enter image description here

When attribute disabled:

enter image description here

enter image description here

Make sure you can get your attribute code in custom_tab.phtml file.

Hope this will help you!

| improve this answer | |
  • Thanks for the answer. one question is do I need to create getWarrantyDisplay() function ? – Dimple Panchal Dec 25 '19 at 8:41
  • no you don't need to create any function. let me know what's your attribute code??? – Naresh Rupareliya Dec 25 '19 at 8:47
  • my attribute code is warranty_display so used getWarrantyDisplay() and warranty_info so used getWarrantyInfo() funstion – Naresh Rupareliya Dec 25 '19 at 8:49
  • yes I have is_warranty_display and warrenty_info – Dimple Panchal Dec 25 '19 at 8:53
  • 1
    so u have to use getIsWarrantyDisplay() instead of getWarrantyDisplay () and getWarrentyInfo() instead of getWarrantyInfo() – Naresh Rupareliya Dec 25 '19 at 8:54
3

Please follow below step to display or remove tab into product detail page.

Create custom tab via module using below reference link

https://www.cloudways.com/blog/add-custom-tab-product-page-magento-2/

Then Create app/code/Cloudways/Mymodule/view/frontend/layout/catalog_product_view.xml file and past below code.

<?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="test.tab" template="Cloudways_Mymodule::custom_tab.phtml" group="detailed_info" >
                <arguments>
                    <argument name="sort_order" xsi:type="string">40</argument>
                    <argument translate="true" name="title" xsi:type="string">Warranty Info</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Then update app/code/Cloudways/Mymodule/view/frontend/templates/custom_tab.phtml file and past below code.

<?php
 $product = $block->getProduct();
 $data = $this->getProduct()->getAttributeText('warranty_display');
?> 
  <?php if($data == 'Yes'): ?>
       <h1 style="color: #1979c3">
        <?php echo $product->getData('sku'); ?>
       </h1>
 <?php endif; ?>
  • Then run setup upgrade command and clear cache.
  • Then check product detail page and verify with your "warranty_display" attribute value
    • if warranty_display value is yes then it's display custom tab same as below enter image description here
    • if warranty_display value is no then custom tab will be removed. enter image description here

I am tested with magento 2.3 please check and let me known if you have any query.

| improve this answer | |
  • Thank you @Bhavin Pethani for the answer, but its not working for me. only content of tab is hiding, not the whole tab. – Dimple Panchal Dec 25 '19 at 12:00
  • Which magento version you are using?, also are you trying custom module or custom theme? – Bhavin Pethani Dec 25 '19 at 12:21
  • Can you tell me which magento version you are using? – Bhavin Pethani Dec 26 '19 at 13:52
  • I am using magento 2.3.3 – Dimple Panchal Dec 27 '19 at 5:19
2

Create these files in your module

Vendor/Module/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="Vendor\Module\Block\MyBlock" template="Vendor_Module::index.phtml" group="detailed_info">
                <arguments>
                    <argument translate="true" name="title" xsi:type="string">Custom Tab</argument>
                    <argument name="sort_order" xsi:type="string">25</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

\Vendor\Module\Block\MyBlock.php

<?php

namespace Vendor\Module\Block;

class MyBlock extends \Magento\Catalog\Block\Product\View\Attributes
{
    public function isCustomAttrAvailable()
    {
        /** @var \Magento\Catalog\Model\Product $product */
        $product = $this->getProduct();

        // Find your attribute from $product variable and return true or false
    }
}

Vendor/Module/view/frontend/templates/index.phtml

<?php if($block->isCustomAttrAvailable()): ?>
    <div class="additional-attributes-wrapper table-wrapper">
        <table class="data table additional-attributes" id="custom-tab">
            <caption class="table-caption"><?= $block->escapeHtml(__('Custom Tab')) ?></caption>
            <tbody>
                <tr>
                    <th>Hello</th>
                    <td>Custom Tab</td>
                </tr>
            </tbody>
        </table>
    </div>
<?php endif; ?>
| improve this answer | |
1

Just override vendor\magento\module-catalog\view\frontend\templates\product\view\details.phtml

to

app\design\frontend\vendor\yourtheme\Magento_Catalog\templates\product\view\details.phtml with below code

<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

// @codingStandardsIgnoreFile

/** @var \Magento\Catalog\Block\Product\View\Details $block */
?>
<?php
//Custom code
$_product = $block->getLayout()->createBlock('Magento\Catalog\Block\Product\View')->getProduct(); ?>
<?php if ($detailedInfoGroup = $block->getGroupSortedChildNames('detailed_info', 'getChildHtml')):?>
    <div class="product info detailed">
        <?php $layout = $block->getLayout(); ?>
        <div class="product data items" data-mage-init='{"tabs":{"openedState":"active"}}'>
            <?php foreach ($detailedInfoGroup as $name):?>
                <?php
                    //Custom code start
                    if($name == 'warrantyInfo.tab'){// Your block name
                        if(!$_product->getDisplayCustomTab()){
                            continue;
                        }
                    }
                    //Custom code end
                    $html = $layout->renderElement($name);
                    if (!trim($html)) {
                        continue;
                    }
                    $alias = $layout->getElementAlias($name);
                    $label = $block->getChildData($alias, 'title');
                ?>
                <div class="data item title"
                     data-role="collapsible" id="tab-label-<?= /* @escapeNotVerified */ $alias ?>">
                    <a class="data switch"
                       tabindex="-1"
                       data-toggle="trigger"
                       href="#<?= /* @escapeNotVerified */ $alias ?>"
                       id="tab-label-<?= /* @escapeNotVerified */ $alias ?>-title">
                        <?= /* @escapeNotVerified */ $label ?>
                    </a>
                </div>
                <div class="data item content"
                     aria-labelledby="tab-label-<?= /* @escapeNotVerified */ $alias ?>-title" id="<?= /* @escapeNotVerified */ $alias ?>" data-role="content">
                    <?= /* @escapeNotVerified */ $html ?>
                </div>
            <?php endforeach;?>
        </div>
    </div>
<?php endif; ?>
| improve this answer | |
1

Finally found the solution for this question.

https://mage2.pro/t/topic/121 worked for me.

So In my block file, wriiten a function that overrides toHtml to blank if my flag is off.

public function toHtml()
{
    return $this->canViewWarrantyTab() ? parent::toHtml() : '';
}

function canViewWarrantyTab() {
    /** @var Product $product */
    $product = $this->attributes->getProduct();

    // Find your attribute from $product variable and return true or false

    if($product->getIsWarrantyDisplay()){
        $this->warranty_info = $product->getWarrentyInfo();
        return true;
    } else {
        return false;
    }
}

Thank you so much all for help me out.

| improve this answer | |

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.