【问题标题】:Add custom button on add/edit product page在添加/编辑产品页面上添加自定义按钮
【发布时间】:2019-07-06 12:17:30
【问题描述】:

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

如何在添加/编辑产品页面 (magento2) 中添加自定义按钮。我需要在点击按钮时打开弹出窗口。

【问题讨论】:

    标签: php xml layout magento2


    【解决方案1】:

    产品表单是通过ui-components生成的。 产品表单的ui组件名称为view/adminhtml/ui_component/product_form.xml

    你需要在自己的模块中创建一个同名同路径的文件,内容如下:

    <?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>
    

    然后在文件[Namespace]/[Module]/Block/Adminhtml/Product/Edit/Button/CustomButton.php中创建类[Namespace]\[Module]\Block\Adminhtml\Product\Edit\Button\CustomButton

    <?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
            ];
        }
    }
    

    您的 ui 组件文件应该与主文件合并,并且您的按钮应该出现在其他按钮中。

    缓存刷新后检查这些更改

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-12-16
      • 2020-01-20
      • 1970-01-01
      • 1970-01-01
      • 2018-02-18
      相关资源
      最近更新 更多