How to create custom tabs in product page magento 2?

Let’s create a product tab in Magento 2 product page using the simple module. We want to display short content for a product using product tabs.

Example, We want to display short description info in product tabs.
Start with the Simple module in Magento 2, create registration.php file to register our module.

Path, app/code/Rbj/ProductTabs/registration.php

File path, app/code/Rbj/ProductTabs/etc/module.xml

We have passed dependency of Catalog module in sequence tag of our module.
Now We want to create catalog_product_view.xml file for display our custom tabs of the short description.
Create a layout file,
Path, app/code/Rbj/ProductTabs/view/frontend/layout/catalog_product_view.xml

We have keep our tab name is Extra Info if you want to change tabs title you can replace Extra info with your title in argument tag.  In the above layout file, We must need to pass group=”detailed_info”  in the block class.

Create template file for call short description content in tabs using template, Path, app/code/Rbj/ProductTabs/view/frontend/templates/short-descripiton.phtml

We need to create ExtraInfo.php file to check if a product has short description we need to display tabs otherwise we don’t need to display empty tabs for the short description.

Path, app/code/Rbj/ProductTabs/Block/ExtraInfo.php

Now run command to activate our module,

You can see new tabs on the product page in the tabs section below the image section. Your new tab will be displayed if your product has the short description otherwise tabs will not display in product page.
You can add new attribute details using the above way.

 

One Reply to “How to create custom tabs in product page magento 2?”

Leave a Reply

Your email address will not be published. Required fields are marked *