Back to Top

Add Custom Button in the Admin Product Form in Magento 2

Updated 17 September 2021

The product form is generated via UI-Components. If we need to add a custom button in admin product form, then it can be possible by the following way.

The UI component name for the product form is :

view/adminhtml/ui_component/product_form.xml.

  1. You need to create a file with the same name and path in your own module with the following content:
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
   <!--For custom button-->
   <argument name="data" xsi:type="array">
       <item name="buttons" xsi:type="array">
           <item name="customButton" xsi:type="string">Webkul\CustomButton\Block\Adminhtml\Product\CustomButton</item>
       </item>
   </argument>
</form>

2. Then create the class in the file

Vendor/Module/Block/Adminhtml/Product/CustomButton.php

<?php
namespace Webkul\CustomButton\Block\Adminhtml\Product;
class CustomButton extends \Magento\Catalog\Block\Adminhtml\Product\Edit\Button\Generic
{
    public function getButtonData()
    {
    	return [
        	'label' => __('Custom Button'),
        	'class' => 'action-secondary',
        	'on_click' => 'alert("Hello Webkul !!")',
        	'sort_order' => 10
    	];
    }
}

Then, Just flush the Cache and your UI-Component file will merge with the main file and your button will appear among the other buttons.

product form

When We click on this button then we have set an alert, it will show.

custom button

That’s all, this is a simple way to add a custom button in the admin product form.

Check this blog if add a custom button in Admin-Grid.

Keep Learning !!

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


2 comments

  • bhim
    • Vaibhav Arora (Moderator)
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home