How to Add Custom Tab View in Magento 2
The tab view provides a manageable way to switch between distinct views by clicking on some of the tabs or by swiping between the views.
The general behavior of the tab view component is to load its items on click. Respectively, the content load and unload events get fired while switching between the views.
If the content is added separately using the menu or a simple link and whenever the users click the menu or the link, it increases the load time of the store. As website optimization is the most significant factor, to boost the speed, one can add custom tab view in Magento 2. Likewise to hide unnecessary tabs from the frontend of product page hide an empty custom tab in Magento 2 to not let distract your customers from purchasing.
Tab view gets loaded immediately on page load. So it’s the best practice to add custom tab view in Magento 2 to optimize the page load time.
Steps to Add Custom Tab View in Magento 2
Well, Magento gives inbuilt tab view facility located in file tabs.js at lib/web/mage/. We can directly call that file in our custom phtml file. Here is the example to add a custom tab view in our phtml file.
1. Add the code given below in your custom phtml file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<div class="product data items" data-mage-init='{"mage/tabs": {"openedState": "active", "animate": {"duration": 100}, "active": 0, "disabled": [], "disabledState": "disabled"}}'> <div class="item title" data-role="collapsible"> <a class="switch" data-toggle="trigger" href="#tab-recent">Recent</a> </div> <div id="tab-recent" class="item content" data-role="content"> </div> <div class="item title" data-role="collapsible"> <a class="switch" data-toggle="trigger" href="#tab-newarrival">New Arrivals</a> </div> <div id="tab-newarrival" class="item content" data-role="content">New Products</div> <div class="item title" data-role="collapsible"> <a class="switch" data-toggle="trigger" href="#tab-wishlist">Wishlist</a> </div> <div id="tab-wishlist" class="item content" data-role="content">Your Wishlist</div> <div class="item title" data-role="collapsible"> <a class="switch" data-toggle="trigger" href="#tab-reviews">Reviews</a> </div> <div id="tab-reviews" class="item content" data-role="content">Your Reviews</div> <div class="item title" data-role="collapsible"> <a class="switch" data-toggle="trigger" href="#tab-purchase">Purchase</a> </div> <div id="tab-purchase" class="item content" data-role="content">Products Purchased</div> </div> |
After implementation of the above tab view, you can see the output as shown in the image.
Ready! Magento 2 create a custom tab and call custom Phtml in product UI component form to develop custom features in the extension.
If you have any doubt regarding this blog, feel free to ask in the Comments section below.
It would be pleasing to help you. Also, if you want to collect additional information from the customers, you can create a tab and load grid in Magento 2 customer admin edit page.
Do consider sharing this post with the Magento community via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
Prev
How to Display Products on Homepage in Magento 2
How to Get Current Store Date and Time in Magento 2
Next