2

I have added the custom attribute tab on the product page. If the tab is empty is must be hidden.

I have created an attribute customtab.

I have added below code in the catalog_product_view.xml inside body tag:

<referenceBlock name="product.info.details">
            <block class="Magento\Catalog\Block\Product\View" name="customtab.tab" template="Magento_Catalog::customtab.phtml" group="detailed_info" >
                <arguments>
                    <argument translate="true" name="title" xsi:type="string">Custom Tab</argument>
                </arguments>
            </block>
        </referenceBlock>

How can I hide the custom attribute tab?

  • 1
    How have you added the tab ? Please update the question with code the code used so we can help you. – Frank Dec 20 '17 at 12:01
  • @Frank, I have updated my question. – Vinod Kumar Dec 20 '17 at 12:41
2

Try following code:

  1. app/code/[VendorName]/[ModuleName]/registration.php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    '[VendorName]_[ModuleName]',
    __DIR__
);
  1. app/code/[VendorName]/[ModuleName]/etc/module.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="[VendorName]_[ModuleName]" setup_version="1.0.0">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>
  1. app/code/[VendorName]/[ModuleName]/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="Magento\Catalog\Block\Product\View" name="custom.tab" template="[VendorName]_[ModuleName]::product/view/details/custom_tab.phtml" group="detailed_info">
                <arguments>
                    <argument translate="true" name="title" xsi:type="string">Tab Title</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>
  1. app/code/[VendorName]/[ModuleName]/view/frontend/templates/product/view/details/custom_tab.phtml
<?php $attributeCode = '<product_attribute_code>'; ?>
<?php echo $block->getProduct()->getData($attributeCode); ?>

Note:

Add your html code in "if" condition, if html data found in tab content then tab will be display on the product page.

  • 1
    Thanks, It is working fine as I want. I have accepted your answer. – Vinod Kumar Dec 21 '17 at 5:22
  • @Vinodkumar - Happy to help. Keep helping :) – Pratik Oza Dec 26 '17 at 9:10
1

Please replace your code with below code. Below code will display attribute in tab if its contain any content/data. (Note: I have used short_description attribute for your reference) Don't forget to remove/clear cache before test.

<referenceBlock name="product.info.details">
                <block class="Magento\Catalog\Block\Product\View\Description" name="product.info.custominfo" template="product/view/attribute.phtml" group="detailed_info">
                    <arguments>
                        <argument name="at_call" xsi:type="string">getShortDescription</argument>
                        <argument name="at_code" xsi:type="string">short_description</argument>
                        <argument name="css_class" xsi:type="string">short_description</argument>
                        <argument name="at_label" xsi:type="string">none</argument>
                        <argument name="title" translate="true" xsi:type="string">Short Details</argument>
                    </arguments>
                </block>              
            </referenceBlock>

I hope above code will help you.

0

Seems to be like you need to add your own custom block class. I'd imagine that the answer provided by Nits wont work for a custom product attribute because nothing does.

<referenceBlock name="product.info.details">
    <block class="Vendor\Module\Block\Product\View\CustomAttributes" name="customtab.tab" template="Magento_Catalog::customtab.phtml" group="detailed_info" >
        <arguments>
            <argument translate="true" name="title" xsi:type="string">Custom Tab</argument>
        </arguments>
    </block>
</referenceBlock>

Take a look at public/vendor/magento/module-review/Block/Product/Review.php as a reference. You will be able to create a method that will use the registry to get the current $product and find out whether your value / values are set. You will then be able to override the toHtml method to output nothing or you can set the setTabTitle() to nothing if not set.

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.