Magento – Magento 2 – Add text before description in product details tab

frontendmagento2product-attribute

I want to add the values of some custom attributes to the contents of the Details tab on the frontend Product page (where the Description is displayed). These values would be placed above the description.

I am not talking about adding a new tab. I want to simply add additional attribute values to an existing tab.

I have added a simple 'echo "TEST"' to THEME/Magento_Catalog/templates/product/view/description.phtml just to try and change the tab contents, but it had no effect. I am pretty new to Magento, so there may be an obvious answer, but I can't seem to figure it out. What am I missing?

Best Answer

vendor/magento/module-catalog/view/frontend/templates/product/view/attribute.phtml

You can override this file in your custom theme and add the custom attribute text before details tab content.

app/design/frontend/VendorName/ThemeName/Magento_Catalog/templates/product/view/attribute.phtml

I have added custom_attribute attribute in my local project and working fine. You can get custom attribute value like this : $_product->getCustomAttribute();

<?php
$_helper = $this->helper('Magento\Catalog\Helper\Output');
$_product = $block->getProduct();
$_call = $block->getAtCall();
$_code = $block->getAtCode();
$_className = $block->getCssClass();
$_attributeLabel = $block->getAtLabel();
$_attributeType = $block->getAtType();
$_attributeAddAttribute = $block->getAddAttribute();

$renderLabel = true;
// if defined as 'none' in layout, do not render
if ($_attributeLabel == 'none') {
    $renderLabel = false;
}

if ($_attributeLabel && $_attributeLabel == 'default') {
    $_attributeLabel = $_product->getResource()->getAttribute($_code)->getStoreLabel();
}
if ($_attributeType && $_attributeType == 'text') {
    $_attributeValue = ($_helper->productAttribute($_product, $_product->$_call(), $_code)) ? $_product->getAttributeText($_code) : '';
} else {
    $_attributeValue = $_helper->productAttribute($_product, $_product->$_call(), $_code);
}
?>
<?php if ($_attributeValue): ?>
    <?php /** This will display custom attribute value **/ ?>
    <?php if ($_code == 'description') : ?>
        <?php echo $_helper->productAttribute($_product, $_product->getCustomAttribute(), 'custom_attribute'); ?>
    <?php endif; ?>

    <?php if ($renderLabel): ?><strong class="type"><?= /* @escapeNotVerified */ $_attributeLabel ?></strong><?php endif; ?>
    <div class="value" <?= /* @escapeNotVerified */ $_attributeAddAttribute ?>><?= /* @escapeNotVerified */ $_attributeValue ?></div>
</div>
<?php endif; ?>

Please clear cache after changing the file. hope this is helpful.