1

How to add product tab using xml in admin? I want to show that tab particular product type.

How can i add this product tab using my custom module in magento 2?

3 Answers 3

2

you can add this in [Namespace]/[Module]/view/adminhtml/layout/catalog_product_new.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="product_tabs">
            <block class="[Namespace]\[module]\Block\Name\Here" name="block.name.here" />
            <action method="addTab">
                <argument name="name" xsi:type="string">block_name_here</argument>
                <argument name="block" xsi:type="string">block.name.here</argument>
            </action>
        </referenceBlock>
    </body>
</page>

then create the block [Namespace]\[module]\Block\Name\Here that implements Magento\Backend\Block\Widget\Tab\TabInterface.

Note:This Solution is working Only Magento 2.0x not in 2.1x

5
  • In magento2.1 i added my custom fields in product edit using below given answer by me but i am not getting that fields value in post after click on save button. Any idea how to get this?
    – ND17
    Commented Aug 1, 2016 at 6:32
  • you just added a line in my answer saying this does not work for 2.1. And I validated your addition. In 2.1, the product add/edit form has been changed from tabs to ui components. This does not work anymore.
    – Marius
    Commented Aug 1, 2016 at 6:39
  • So is it not possible to add custom fields using phtml?
    – ND17
    Commented Aug 1, 2016 at 6:42
  • I don't know. Most probably not. You can check how the tabs are added in the ui component for products and try to replicate the behavior.
    – Marius
    Commented Aug 1, 2016 at 6:57
  • Just got it.In input field add this data-form-part="product_form" and i get value of my custom fields
    – ND17
    Commented Aug 1, 2016 at 7:06
3

Add xml file to Companyname/Modulename/view/frontend/layout/catalog_product_view.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">    
    <body>        
        <referenceBlock name="product.info.details">
            <block class="Companyname\Modulename\Block\yourblockname" name="mycustom.tab" as="mycustom" template="Companyname_Modulename::mytab.phtml" group="detailed_info">               
                <arguments>                       
                        <argument name="title" translate="true" xsi:type="string">My Custom Tab Name</argument>
                  </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

Add you template file to Companyname/Modulename/view/frontend/templates/mytab.phtml

Hope it will helpful to you.

0
1

This Answer is for Magento 2.1.0

In catalog_product_new.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
   <body>
      <referenceContainer name="product_form">
        <block name="class_session" class="[Namespace]\[module]\Block\Adminhtml\Catalog\Product\Edit\Tab\Tabname">
            <arguments>
                <argument name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Write Name</item>
                    <item name="collapsible" xsi:type="boolean">true</item>
                    <item name="opened" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">33</item>
                    <item name="canShow" xsi:type="boolean">true</item>
                    <item name="componentType" xsi:type="string">fieldset</item>
                </argument>
            </arguments>
        </block>
    </referenceContainer>
  </body>
</page>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.