0

I created a custom product tab that I want to use for a specific attribute set. For that attribute set I have created a number of specific attributes.

The custom product tab works nice. However it is visible for every product, which is not wanted. How could I make sure that the tab is only visible for a specific attribute set (or product attributes for that matter).

My code:

  1. app/code/[VendorName]/[ModuleName]/registration.php

    <?php
    
    \Magento\Framework\Component\ComponentRegistrar::register(
        \Magento\Framework\Component\ComponentRegistrar::MODULE,
        'Mark_Eopyy',
        __DIR__
    );
    
  2. app/code/[VendorName]/[ModuleName]/etc/module.xml

    <?xml version="1.0"?>
    
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
        <module name="Mark_Eopyy" setup_version="1.0.0">
    
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    
    
        </module>
    </config>
    
  3. app/code/[VendorName]/[ModuleName]/view/frontend/layout/catalog_product_view.xml

    <?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="Mark_Eopyy::custom_tab.phtml" group="detailed_info">
                    <arguments>
                        <argument translate="true" name="title" xsi:type="string">ΕΟΠΥΥ</argument>
                    </arguments>
                </block>
            </referenceBlock>
        </body>
    </page>
    
  4. app/code/[VendorName]/[ModuleName]/view/frontend/templates/product/view/details/custom_tab.phtml

    <?php
    $product = $block->getProduct();
    ?>
    <h1 style="color: #1979c3"><?php echo $product->getData('product_attribute'); ?></h1>
    

Thank you

0

I found a solution by wrapping the html part of the template file in a PHP if statement.

Code:

<?php
$product = $block->getProduct();
?>
<?php if( $product->getData('product_attribute') ): ?>
<h1 style="color: #1979c3"><?php echo $product->getData('product_attribute'); ?></h1>


<?php endif; ?>

However I don't tick this as solved yet, in case someone points to a more elegant solution.

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.