Magento2.2 add custom product tab with custom template on product edit page

In this blogpost we will learn how to add custom tab in product edit page in admin panel.Create a file with mentioned path and add the following code:-

view\adminhtml\ui_component\product_form.xml
 <form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <htmlContent name="qrcode" sortOrder="22">
        <argument name="data" xsi:type="array">
            <item name="wrapper" xsi:type="array">
                <item name="label" xsi:type="string" translate="true">Qrcode</item>
                <item name="collapsible" xsi:type="boolean">true</item>
                <item name="opened" xsi:type="boolean">false</item>
            </item>
        </argument>
        <settings>
            <wrapper>
                <canShow>true</canShow>
                <componentType>fieldset</componentType>
            </wrapper>
        </settings>
        <block class="W3solver\Qrcode\Block\Adminhtml\Catalog\Product\Edit\Tab\Qrcode" name="qrcode">
            <arguments>
                <argument name="config" xsi:type="array">
                    <item name="label" xsi:type="string" translate="true">Qrcode</item>
                    <item name="collapsible" xsi:type="boolean">true</item>
                    <item name="opened" xsi:type="boolean">false</item>
                    <item name="sortOrder" xsi:type="string">11</item>
                    <item name="canShow" xsi:type="boolean">true</item>
                    <item name="componentType" xsi:type="string">fieldset</item>
                </argument>
            </arguments>
        </block>
    </htmlContent>
</form>

Lets understand above code. product_form is used for customization of product edit page in admin panel as per magento documentation(for more info refer the link). So above code will add some htmlcontent which will thus render a block to show your custom tab and its template.

Now lets create our block at below path

 W3solver\Qrcode\Block\Adminhtml\Catalog\Product\Edit\Tab\Qrcode.php 
<?php
/**
 * W3solver.com
 * @category    W3solver
 * @package     W3solver_Qrcode
 * @Description Block to add qrcode tab to product edit page
 * @author      W3solver
 * @copyright   Copyright (c) 2017 w3solver
 */
namespace W3solver\Qrcode\Block\Adminhtml\Catalog\Product\Edit\Tab;

use Magento\Backend\Block\Template\Context;
use Magento\Framework\Registry;
use W3solver\Qrcode\Model\QrcodeListFactory;

class Qrcode extends \Magento\Framework\View\Element\Template {

    protected $_template = 'product/edit/qrcode.phtml';
    protected $_qrcodeListFactory;
    protected $fileName;

    /**
     * Core registry
     *
     * @var Registry
     */
    protected $_coreRegistry = null;

    /**
     * @param Magento\Backend\Block\Template\Context
     * @param Magento\Framework\Registry
     * @param W3solver\Qrcode\Model\QrcodeListFactory
     * @param Magento\Store\Model\StoreManagerInterface
     */
    public function __construct(
    Context $context, Registry $registry, QrcodeListFactory $qrcodeListFactory, array $data = []
    ) {
        $this->_coreRegistry = $registry;
        $this->_qrcodeListFactory = $qrcodeListFactory;
        
        parent::__construct($context, $data);
    }

    /**
     * Retrieve product
     *
     * @return \Magento\Catalog\Model\Product
     */
    public function getProduct() {
        return $this->_coreRegistry->registry('current_product');
    }
}

We also need to create our template file to show whatever content we want at following path:-

w3solver\Qrcode\view\adminhtml\templates\product\edit\qrcode.phtml

So above code will craete a Qrcode tab on product edit page in admin. Drop in your comments if you face any issue.