Adding Custom Tabs to Magento Product Pages
Welcome! Today we’re going to explore how to add custom tabs to your Magento product pages. I’ll guide you through both changing default tab names and adding entirely new tabs with custom attributes.
Changing Default Tab Names in Magento
Let’s start by figuring out where those default tab names are defined. In most cases, you’ll find them in the <template> folder of your theme under catalog/product/view.phtml.
Here’s an example of what the default tab definitions look like:
<div class="product-collateral tabs">
<ul id="collateral-tabs" class="nav nav-tabs">
<li><a href="#tab-description"><?php echo $this->__('Product Description') ?></a></li>
<li><a href="#tab-reviews"><?php echo $this->__('Reviews') ?></a></li>
</ul>
<div id="tab-description" class="active tab-content">
<?php if ($description = $this->getProduct()->getDescription()): ?>
<div class="std"><?php echo $description ?></div>
<?php else: ?>
<p><?php echo $this->__('There is no description for this product.') ?></p>
<?php endif; ?>
</div>
<div id="tab-reviews" class="tab-content">
<p><?php echo $this->getReviewsSummaryHtml() ?></p>
</div>
</div>
To change a tab name, simply modify the echo statement within your desired <li> element:
<li><a href="#tab-description"><?php echo $this->__('Custom Description') ?></a></li>
However, if you’re like me and can’t remember where you made this change, it might be in an extension or custom theme override. Check the app/code/local directory for any overrides.
Adding New Custom Tabs
Now let’s add a completely new tab to our product page. For this example, we’ll assume you have a custom attribute called “Additional Information”.
First, define your new tab in the same view.phtml file:
<li><a href="#tab-additional-info"><?php echo $this->__('Additional Information') ?></a></li>
<div id="tab-additional-info" class="tab-content">
<p><?php echo $this->getProduct()->getAttributeText('additional_info'); ?></p>
</div>
Next, make sure your new attribute is enabled for display on the product page:
- In Magento Admin, navigate to Catalog > Attributes.
- Find “Additional Information” in the list and click Edit.
- Under “Storefront Properties”, set “Visible on Product View Page On Frontend” to “Yes”.
- Save your changes.
Finally, ensure your theme includes the necessary JavaScript for the tabs:
<script type="text/javascript">
require(['jquery', 'mage/tabs'], function($) {
$('#collateral-tabs').tabs();
});
</script>
Conclusion
And there you have it! Adding custom tabs to Magento product pages is a straightforward process once you understand where the default definitions are located.
If you’d like to learn more about extending Magento functionality with custom attributes and tabs, check out our Magento Extension Development Guide.
Remember, always make sure your changes are properly tested in a staging environment before deploying them to your live store. Happy coding!