0

I have used the following code

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

to create a module that adds a custom tab to our product pages. It works perfectly but I need to somehow change the module so that the custom tab only display on products in certain categories. Has anyone any suggestions on how to do this? Don't mind if the category numbers need to be hard coded as there won't be many! Please note we are using Magento 2.2.1

David

1 Answer 1

0

Try replacing code after <?php if ($_attributeValue): ?>, in attribute.phtml with following:

<?php if ($_attributeValue): ?>
<?php if ($_attributeType === 'your_type'):
    $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
    $category = $objectManager->get('Magento\Framework\Registry')->registry('current_category');
    if (strtolower($category->getName()) === 'category_to_display'): ?>
        <?php /* @escapeNotVerified */ echo $_attributeValue; ?> <!-- add your tab logic here -->
    <?php endif; ?>
<?php else: ?>
<div class="product attribute <?php /* @escapeNotVerified */ echo $_className?>">
    <?php if ($_attributeLabel != __('none')): ?><strong class="type"><?php /* @escapeNotVerified */ echo $_attributeLabel?></strong><?php endif; ?>
    <div class="value" <?php /* @escapeNotVerified */ echo $_attributeAddAttribute;?>><?php /* @escapeNotVerified */ echo $_attributeValue; ?></div>
</div>
<?php endif; ?>    
<?php endif; ?>

change your_type to your declared attribute type, change category_to_display to your desired category name (chain || to add more categories to include tab). Add your code to add your tab logic here

2
  • Adrian,I believe attribute.phtml only exists in Magento 1 (?), we're using Magento 2.2.1 (question updated to reflect) Commented Dec 11, 2017 at 14:06
  • Hi David, I wrote answer on Magento 2.1. I'm pretty sure you should have this file on 2.2 too. Check ./vendor/magento/module-catalog/view/frontend/templates/product/view/attribute.phtml
    – adyry
    Commented Dec 11, 2017 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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