Here is my etc/adminhtml/events.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="adminhtml_catalog_product_edit_prepare_form">
<observer name="custom_product_fields" instance="Vendor\Module\Observer\CatalogProductEditPrepareForm" />
</event>
Here is my Observer/CatalogProductEditPrepareForm.php
<?php
namespace Vendor\Module\Observer;
use Magento\Framework\Event\ObserverInterface;
class CatalogProductEditPrepareForm implements ObserverInterface
{
protected $_coreRegistry;
/**
* @param \Magento\Framework\Registry $coreRegistry
*/
public function __construct(
\Magento\Framework\Registry $coreRegistry
) {
$this->_coreRegistry = $coreRegistry;
}
/**
* product from prepare event handler.
* @param \Magento\Framework\Event\Observer $observer
* @return $this
*/
public function execute(\Magento\Framework\Event\Observer $observer)
{
$form = $observer->getForm(); // $observer contain form object
// get your group element in which you want to add custom filed
$fieldset = $form->getElement('product-details');
if ($fieldset) {
// get current product if you want to use any data from product
$product = $this->_coreRegistry->registry('product');
$fieldset ->addField(
'custom-field-1',
'text',
[
'name' => 'custom-field-1',
'label' => __('Custom Field 1'),
'id' => 'custom-field-1'
]
);
$fieldset ->addField(
'custom-field-2',
'text',
[
'name' => 'custom-field-2',
'label' => __('Custom Field 2'),
'id' => 'custom-field-2'
]
);
// You can set any data in these elements for display default value
$form->addValues(
[
'custom-field-1' => 'data according to you', // ex. $product->getName()
'custom-field-2' => 'data according to you'
]
);
}
return $this;
}
}
I am trying to add custom field into product details tab. seems my event observer is not working. Please anyone help on this. Thanks