0

how do I add a tab in the admin panel under the Product Details, assuming that I already made a module and enabled it. I just can't find the right place Screenshot applied:

enter image description here

Just like add a tab from anywhere and attach the tab to template file, block, etc. Thank you.

  • By hand, or by code? – Ryan Hoerr Feb 24 '16 at 13:39
  • by code if possible. because I will need to do that a lot and, I need to create lots of tabs attached to new "pages" them, and I see the change of the content is done by javascript or something. If you can explain how to add it by code and how to attach a template to it, it would be awesome and a huge help for future modifications and adding new functionality. – Lachezar Raychev Feb 24 '16 at 13:45
  • if you can tell me how I can add it by hand that will also be some kind of a start. – Lachezar Raychev Feb 24 '16 at 16:06
  • A lot of things in Magento are complicated, but there is (almost) always a reason. – Ryan Hoerr Feb 26 '16 at 14:18
  • yeah, sure, lets hope I will love it one day... doubt it :) – Lachezar Raychev Feb 26 '16 at 14:40
4

There are two ways to add tabs to the product edit page. Tabs are directly associated to attribute set groups.

To add from the admin interface:

  1. Go to Admin > Stores > Attributes > Attribute Set.
  2. Select your attribute set (default, or whatever the case).
  3. Under the center column 'Groups', click 'Add New'.
  4. Enter your group name, then add attributes to it.

This will add a tab corresponding to the group.

To do it by code:

Create a module, if you don't already have one.

Create a class {module}\Setup\InstallData:

<?php

namespace My\Module\Setup;

use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;

/**
 * Install attributes
 */
class InstallData implements \Magento\Framework\Setup\InstallDataInterface
{
    /**
     * @var \Magento\Catalog\Setup\CategorySetupFactory
     */
    protected $categorySetupFactory;

    /**
     * Init
     *
     * @param \Magento\Catalog\Setup\CategorySetupFactory $categorySetupFactory
     */
    public function __construct(
        \Magento\Catalog\Setup\CategorySetupFactory $categorySetupFactory
    ) {
        $this->categorySetupFactory = $categorySetupFactory;
    }

    /**
     * Installs data for a module
     *
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     * @return void
     */
    public function install(ModuleDataSetupInterface $setup, ModuleContextInterface $context)
    {
        /** @var \Magento\Catalog\Setup\CategorySetup $categorySetup */
        $categorySetup = $this->categorySetupFactory->create(['setup' => $setup]);

        $setup->startSetup();

        $categorySetup->addAttribute(
            \Magento\Catalog\Model\Product::ENTITY,
            'custom_attribute',
            [
                'type'                  => 'int',
                'label'                 => 'Custom Attribute',
                'input'                 => 'select',
                'source'                => 'Magento\Eav\Model\Entity\Attribute\Source\Boolean',
                'sort_order'            => 100,
                'global'                => \Magento\Eav\Model\Entity\Attribute\ScopedAttributeInterface::SCOPE_STORE,
                'group'                 => 'My Custom Tab', // If this does not exist, a new group will be created.
                'is_used_in_grid'       => true,
                'is_visible_in_grid'    => true,
                'is_filterable_in_grid' => true,
                'used_for_promo_rules'  => true,
                'required'              => false,
            ]
        );

        $setup->endSetup();
    }
}

This will add a yes/no product attribute, and assign it to attribute group 'My Custom Tab'. The label can be anything.

If you want a custom for your form field(s), you could define your own input renderer for the attribute.

You can also add a group explicitly, and place it within the 'advanced' section, like so:

// Add new tab
$entityTypeId   = $categorySetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
$attributeSetId = $categorySetup->getAttributeSetId($entityTypeId, 'Default');

$categorySetup->addAttributeGroup($entityTypeId, $attributeSetId, 'My Custom Tab', 65);
$categorySetup->updateAttributeGroup(
    $entityTypeId,
    $attributeSetId,
    'My Custom Tab',
    'attribute_group_code',
    'my_custom_tab'
);
$categorySetup->updateAttributeGroup(
    $entityTypeId,
    $attributeSetId,
    'My Custom Tab',
    'tab_group_code',
    'advanced'
);
  • thank you! I will investigate this in more depth... I was hoping that I won't have to put it in the database... since I will have multiple stores and wanted to just enable/disable module and the tab would disappear, without it being in the DB, but I guess there is no way around that. Anyway, thank you for your time and help! – Lachezar Raychev Feb 26 '16 at 15:26
  • dude! i have such a better understanding of how tabs are connected to the attributes now! That was not at all what I needed to do the task I want :) :) BUT HUGE THANKS! :) – Lachezar Raychev Feb 26 '16 at 16:09
  • this is for attribute in tab but i want to set template in thattab how caan i do that? – ND17 Mar 18 '16 at 7:54
0

You can add the tab with the custom template as below.

app\code\Magentoins\Prices\view\adminhtml\layout\catalog_product_new.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <update handle="editor"/>
    <body>
        <referenceBlock name="product_tabs">            
            <block class="Magentoins\Prices\Block\Adminhtml\Product\Edit\Tab\Prices" name="product-prices"/>
            <action method="addTab">
                <argument name="name" xsi:type="string">product-prices</argument>
                <argument name="block" xsi:type="string">product-prices</argument>
            </action>             
        </referenceBlock>
    </body>
</page>

app\code\Magentoins\Prices\Block\Adminhtml\Product\Edit\Tab\Prices.php

<?php
namespace Magentoins\Prices\Block\Adminhtml\Product\Edit\Tab;
class Prices extends \Magento\Backend\Block\Widget\Form\Generic implements \Magento\Backend\Block\Widget\Tab\TabInterface
{
    /**
     * Reference to product objects that is being edited
     *
     * @var \Magento\Catalog\Model\Product
     */
    protected $_product = null;

    /**
     * @var \Magento\Framework\DataObject|null
     */
    protected $_config = null;

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


    public function __construct(
        \Magento\Backend\Block\Template\Context $context,
        \Magento\Framework\Registry $registry,
        \Magento\Framework\Data\FormFactory $formFactory,
        \Magentoins\Prices\Helper\Data $helperData,
        array $data = []
    ) {
        $this->_coreRegistry = $registry;
        $this->_formFactory = $formFactory;        
        parent::__construct($context, $registry, $formFactory, $data);
    }

    /**
     * Check is readonly block
     *
     * @return boolean
     */
    public function isReadonly()
    {
        return false;
    }

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

    /**
     * Get tab label
     *
     * @return \Magento\Framework\Phrase
     */
    public function getTabLabel()
    {
        return __('Prices');
    }

    /**
     * Get tab title
     *
     * @return \Magento\Framework\Phrase
     */
    public function getTabTitle()
    {
        return __('Prices Information');
    }

    /**
     * Check if tab can be displayed
     *
     * @return boolean
     */
    public function canShowTab()
    {
        return true;
    }

    /**
     * Check if tab is hidden
     *
     * @return boolean
     */
    public function isHidden()
    {
        return false;
    }

    /**
     * Get downloadable tab content id
     *
     * @return string
     */
    /*public function getContentTabId()
    {
        return 'tab_content_prices';
    }*/

    /**
     * @return Form
     */
    protected function _prepareForm()
    {        
        return parent::_prepareForm();
    }

}

app\code\Magentoins\Prices\Block\Adminhtml\Product\Edit\Tab\Price\Prices.php

<?php
namespace Magentoins\Prices\Block\Adminhtml\Product\Edit\Tab\Prices;

class Prices extends \Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Price\Tier
{
    /**
     * Reference to product objects that is being edited
     *
     * @var \Magento\Catalog\Model\Product
     */
    protected $_product = null;

    /**
     * @var \Magento\Framework\DataObject|null
     */
    protected $_config = null;

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

    /**
     * @var string
     */
    protected $_template = 'catalog/product/edit/prices.phtml';

    /**
     * @return $this
     */
    protected function _prepareLayout()
    {
        parent::_prepareLayout();       

    }

}
0

Slava/Yurthev/view/adminhtml/layout/catalog_product_edit.xml

<?xml version="1.0"?>
<!--
/**
 * @author SlavaYurthev
 */
-->
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-2columns-left" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceContainer name="product_form">
            <block name="example" class="Slava\Yurthev\Block\Adminhtml\Example">
                <arguments>
                    <argument name="config" xsi:type="array">
                        <item name="label" xsi:type="string" translate="true">Example</item>
                        <item name="collapsible" xsi:type="boolean">true</item>
                        <item name="opened" xsi:type="boolean">false</item>
                        <item name="sortOrder" xsi:type="string">100</item>
                        <item name="canShow" xsi:type="boolean">true</item>
                        <item name="componentType" xsi:type="string">fieldset</item>
                    </argument>
                </arguments>
            </block>
        </referenceContainer>
    </body>
</page>

Slava/Yurthev/Block/Adminhtml/Example.php

<?php
/**
 * @author SlavaYurthev
 */
namespace Slava\Yurthev\Block\Adminhtml\Example;

class Hints extends \Magento\Backend\Block\Widget\Form\Generic {
    public function toHtml(){
        return 'your tab html';
    }
}
  • this is not working in magento 2.2.2 – Siva Kumar Koduru Jan 19 '18 at 12:11
  • Tab not showing or your additional data not sending? For data sending your fields should has data-part="product_form" attribute – Slava Yurthev Jan 29 '18 at 9:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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