Magento – How to create a custom product tab in admin panel of Magento 2.1

magento-2.1

I want to create a custom tab in back-end product edit module of Magento 2.1. Like this

http://i.stack.imgur.com/ih8Wb.png

I tried this and tried to know how Magento adds these tabs. But didn't find a solution.

Please help me find the correct solution.

Best Answer

Magento 2.1 :- You can use this code for simple custom tab add in product edit page. Create Own module and put below code in Vendor/Module/view/adminhtml/layout/catalog_product_new.xml

<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_form">
            <block class="Vendor\Module\Block\Adminhtml\Product\Edit\Tab\Welcome" name="product.welcome" as="custom-tab" >
                <arguments>
                    <argument name="config" xsi:type="array">
                        <item name="label" xsi:type="string" translate="true">Product Welcome</item>
                        <item name="collapsible" xsi:type="boolean">true</item>
                        <item name="opened" xsi:type="boolean">true</item>
                        <item name="sortOrder" xsi:type="string">2</item>
                        <item name="canShow" xsi:type="boolean">true</item>
                        <item name="componentType" xsi:type="string">fieldset</item>
                    </argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>

In block Vendor/Module/Block/Adminhtml/Product/Edit/Tab/Welcome.php put below code.

<?php

namespace Vendor\Module\Block\Adminhtml\Product\Edit\Tab;
use Magento\Backend\Block\Template\Context;
use Magento\Framework\Registry;

class Welcome extends \Magento\Framework\View\Element\Template
{
    protected $_template = 'catalog/product/edit/welcome.phtml';

    protected $_coreRegistry = null;

    public function __construct(
        Context $context,
        Registry $registry,
        array $data = []
    )
    {
        $this->_coreRegistry = $registry;
        parent::__construct($context, $data);
    }

    public function getProduct()
    {
        return $this->_coreRegistry->registry('current_product');
    }
}

In templates Vendor/Module/view/adminhtml/templates/catalog/product/edit/welcome.phtml put below code.

<div class="welcome">
    <?php echo __('Welcome !'); ?>
</div>

Now Check back-end product module of Magento 2.1. It's working perfect.