Add custom button on add/edit product page

ⅰ亾dé卋堺 提交于 2020-05-16 11:29:10

问题


https://i.stack.imgur.com/uHZ33.png

How to add a custom button, in add/edit product page (magento2). I need to open popup on clicking of the button.


回答1:


The product form is generated via ui-components. The ui component name for product form is view/adminhtml/ui_component/product_form.xml.

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">
    <argument name="data" xsi:type="array">
        <item name="buttons" xsi:type="array">
            <item name="button-unique-identifier-here" xsi:type="string">[Namespace]\[Module]\Block\Adminhtml\Product\Edit\Button\CustomButton</item>
        </item>
    </argument>
</form>

Then create the class [Namespace]\[Module]\Block\Adminhtml\Product\Edit\Button\CustomButton in the file [Namespace]/[Module]/Block/Adminhtml/Product/Edit/Button/CustomButton.php

<?php 
namespace [Namespace]\[Module]\Block\Adminhtml\Product\Edit\Button;

use Magento\Catalog\Block\Adminhtml\Product\Edit\Button\Generic;

class CustomButton extends Generic
{
    public function getButtonData()
    {
        return [
            'label' => __('Your button label here'),
            'on_click' => "alert('it works')",
            'sort_order' => 100
        ];
    }
}

Your ui component file should be merged with the main file and your buttons should appear among the other buttons.

Check these changes after cache flush



来源:https://stackoverflow.com/questions/54657067/add-custom-button-on-add-edit-product-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!