1

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

0

Im not getting why you are adding form with the Observer . What version of magento 2 do you have ? Even if you are able to add them that way, how can you save the form ?

You can follow up this link and add forms with uicomponent Add new tab in product edit page in admin Magento 2 , there are answers for magento 2.1 and 2.2 . The Webkul blog you are refering https://webkul.com/blog/add-custom-form-elements-product-addedit-form-specific-group-section-magneto2/ ;) dates since 2 June 2016 .

If you have a magento 2 older version and you insist that your code may work , then check the module is enable , make some dies and vardumps . But i suggest you to go with the uicomponents way.

  • yes i followed the link you have given me, from that i can load the custom phtml file content, but the input and value how can we set there? And also can we move the content to product edit form instead of creating new fieldset? – jafar pinjar Nov 8 '18 at 13:24
  • I am using magento 2.2.4, And i am looking for code how we can add custom field to product detail tab instead of creating custom field set – jafar pinjar Nov 8 '18 at 13:30
  • @jafar pinjar have you get any solution? – kim na na Dec 31 '18 at 6:32
  • @kimnana, yes its worked for me – jafar pinjar Dec 31 '18 at 6:53
  • which solution you used ? is observer or by using link ? can you share all details. I Just stuck at save checkbox field data or other fields data. – kim na na Dec 31 '18 at 7:09

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.