Add Custom Product Tabs to Magento: A Complete Guide
In this article
Adding a custom tab to a Magento product page allows you to present critical information—such as size charts, technical specifications, warranty details, or installation guides—without cluttering the primary product description. By leveraging Magento's layout XML system and the Block/Template architecture, you can inject new content areas into the existing product details tabs (typically the 'Details' and 'Reviews' section) to improve user experience and conversion rates.
What You'll Need
- Access to your Magento server via SSH or an FTP client (like FileZilla).
- A code editor (such as VS Code or PHPStorm) for editing XML and PHTML files.
- Basic knowledge of the Magento 2 directory structure.
- Administrator access to the Magento backend to clear caches.
- A custom module already created or the ability to create one (it is strongly discouraged to modify core files directly).
Step-by-Step
- Create a Custom Module: To ensure your changes are upgrade-safe, you must place your code in a custom module. If you haven't already, create the directory structure:
app/code/VendorName/ModuleName. Inside this, create theregistration.phpandetc/module.xmlfiles to register the module with the system. - Define the Layout XML: You need to tell Magento to add a new block to the product page. Create a layout file at
app/code/VendorName/ModuleName/view/frontend/layout/catalog_product_view.xml. This specific filename targets the product view page. - Configure the XML Block: Inside the
catalog_product_view.xmlfile, you will use thetag. Theproduct.info.detailsblock is the container for the tabs. You will add aelement inside this reference. You must give your block a unique name, specify the class (usuallyMagento\Framework\View\Element\Template), and point it to a template file using thetemplateattribute (e.g.,VendorName_ModuleName::product/tab.phtml). - Set the Tab Title: In the XML block definition, you can pass arguments to the block. Use the
` tag to define a title. This title is what the customer will see on the tab header. For example, if you are adding a "Shipping Info" tab, set the title argument to "Shipping Information". - Create the Template File: Now, create the PHTML file you referenced in the XML:
app/code/VendorName/ModuleName/view/frontend/templates/product/tab.phtml. This is where the actual HTML content of your tab will live. You can hardcode text here, or better yet, use PHP to pull data from a custom product attribute. - Fetching Dynamic Data (Optional): If your tab should show different content for different products, create a custom product attribute in the Magento Admin (Stores > Attributes > Product). In your
tab.phtmlfile, use$block->getProduct()to retrieve the current product object and then call$product->getData('your_attribute_code')to display the specific value for that product. - Register the Module and Clear Cache: Run the following commands in your terminal from the Magento root directory:
bin/magento setup:upgrade,bin/magento setup:di:compile, andbin/magento cache:flush. This ensures the system recognizes the new layout instructions and the new module. - Verify the Implementation: Navigate to a product page on the frontend of your store. You should now see your custom tab alongside the standard 'Details' and 'Reviews' tabs. Click the tab to ensure your PHTML content renders correctly.
Common Mistakes
One of the most frequent errors is attempting to modify the catalog_product_view.xml file directly within the Magento_Catalog core module. Doing this makes it impossible to update Magento without losing your changes and can lead to severe stability issues. Always use a separate vendor module.
Another common pitfall is forgetting to run the setup:upgrade command. Because layout changes are cached and module registrations are stored in the database, simply refreshing the browser will not show the new tab. If the tab doesn't appear, the first check should always be the cache and the module status.
Developers often forget to handle empty attributes. If you are pulling data from a custom attribute and that attribute is empty for a specific product, the tab might still appear but remain blank, which looks broken to the customer. It is best practice to wrap the entire block in a conditional check in the PHTML file to ensure the tab only renders if there is content to show.
Incorrect XML syntax is also a major hurdle. A single missing closing tag in catalog_product_view.xml can cause the entire product page to crash or fail to load, often resulting in a generic "An error occurred" message on the frontend without a clear explanation.
Tips
To make your tabs more flexible, consider using a CMS block instead of a hardcoded PHTML file. You can create a CMS block in the admin panel and then call that block within your custom tab template. This allows non-technical store managers to update the tab content without needing a developer to change the code.
For better performance, avoid performing heavy database queries directly inside the PHTML template. If you need complex data, use a ViewModel or a Block class to handle the logic and simply pass the processed data to the template for rendering.
If you want to change the order of the tabs, you can use the sort_order argument in your XML block definition. A lower number will move the tab further to the left, allowing you to prioritize your custom information over the default description tab.
Consider using a translation wrapper __('Your Tab Title') in your XML or PHTML files. This ensures that if you expand your store to multiple languages, your custom tab titles can be easily translated via CSV translation files.
Troubleshooting
If the tab does not appear after following the steps, first check the var/log/system.log and var/log/exception.log files. These logs often reveal XML parsing errors or missing class dependencies that prevent the layout from loading.
If the tab is visible but the content is missing, verify the path to your PHTML file. Magento is very strict about the directory structure; a typo in the folder name (e.g., template instead of templates) will result in a blank output or a "template not found" error in developer mode.
If you see the tab but the styling is broken, it is likely because the custom content is not wrapped in the standard Magento CSS classes. Inspect the page source of the existing 'Details' tab and ensure your HTML structure mirrors the container classes used by the Luma or your specific custom theme.
In cases where the tab appears on some products but not others, double-check the attribute visibility settings. Ensure the custom attribute is assigned to the correct Attribute Set and that the "Visible on Product View Page on Front-end" setting is configured correctly, although the manual XML method usually bypasses this, it is good for consistency.
Frequently Asked Questions
Can I add multiple custom tabs?
Yes, you can add as many tabs as needed by defining multiple <block> elements within the product.info.details reference in your layout XML file.
How do I make a custom tab the default open tab?
Magento typically opens the first tab by default. To make your custom tab the default, give it the lowest sort_order value so it appears first in the list.
Does this work for both Simple and Configurable products?
Yes, since catalog_product_view.xml targets the general product view page, the tab will appear on all product types unless you specifically use layout handles to restrict it.
Can I hide the default 'Details' tab?
Yes, you can use <referenceBlock name="product.info.details" remove="true"> or a more targeted <referenceBlock name="product.info.description" remove="true"> to hide the default description tab.
Can I add images or videos to the custom tab?
Absolutely. Since the tab content is rendered via a PHTML template, you can include any valid HTML, including <img> tags, iframe embeds for YouTube, or custom JavaScript sliders.
Will this affect my site speed?
Adding a simple tab with text or a CMS block has a negligible impact on performance. However, avoid loading large external scripts or unoptimized images within the tab to keep page load times fast.