Magento 1 Tutorials Add Custom Tabs to the Magento 2 Product Admin
This article has been updated for Magento 2.
Let's add a new tab to the Magento 2 product admin edit page. You can use this tab to add any information or fields that you need and then save the data when the product form is submitted (the save button is clicked).
This article assumes you have a module setup using the name FishPig_CustomProductTab and all file paths are relative to this. You will need to modify accordingly to use this code in your own module.
<?xml version="1.0" encoding="UTF-8"?> <!-- 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"> <fieldset name="your_tab_id" sortOrder="40"> <settings> <collapsible>true</collapsible> <label translate="true">Custom Tab</label> </settings> <container name="your_tab_id.container" sortOrder="160"> <htmlContent name="html_content"> <block name="your_custom_tab" class="FishPig\CustomProductTab\Block\Adminhtml\Product\CustomTab" /> </htmlContent> </container> </fieldset> </form>
Add A Custom Product Admin Tab with a Blank .phtml Template
One option is to add a tab that just uses a custom .phtml template file. You can add anything you want in here.
<?php /** * @path Block\Adminhtml\Product\CustomTab.php */ namespace FishPig\CustomProductTab\Block\Adminhtml\Product; use Magento\Framework\Json\Helper\Data as JsonHelper; use Magento\Directory\Helper\Data as DirectoryHelper; class CustomTab extends \Magento\Backend\Block\Template { /** * */ protected $_template = 'FishPig_CustomProductTab::custom-tab.phtml'; /** * */ public function __construct( \Magento\Backend\Block\Template\Context $context, \Magento\Framework\Registry $registry, array $data = [] ) { $this->registry = $registry; parent::__construct($context, $data); } /** * @return \Magento\Catalog\Model\Product */ public function getProduct() { return $this->registry->registry('product') } }
<?php /** * @path view/adminhtml/templates/custom-tab.phtml */ ?> <div>You can enter your custom tab content here. The current product is <strong><?= $block->getProduct()->getName() ?></strong></div>
Add A Custom Product Admin Tab with Form Fields
Another option is to make use of the Form widget and use PHP to build a form. This allows Magento to handle the HTML for you and makes it easy to get something set up that looks great.
<?php /** * */ namespace FishPig\CustomProductTab\Block\Adminhtml\Product; class CustomTab extends \Magento\Backend\Block\Widget\Form\Generic { /** * @return \Magento\Catalog\Model\Product */ public function getProduct() { return $this->_coreRegistry->registry('product'); } /** * */ protected function _prepareForm() { $product = $this->getProduct(); $form = $this->_formFactory->create(); $fieldset = $form->addFieldset('your_custom_fieldset', []); $fieldset->addField( 'default_price_test', 'text', [ 'label' => __('Custom Field'), 'title' => __('Custom Field'), 'name' => 'custom_field', 'bold' => true, 'value' => $product->getCustomField() // Get the custom value here ] ); $this->setForm($form); return parent::_prepareForm(); } }