Magento 2:在订单详情页添加自定义按钮并实现功能

magento 2:在订单详情页添加自定义按钮并实现功能

本文档详细介绍了如何在 Magento 2 后台的订单详情页面添加一个自定义按钮,并配置其点击后的功能。通过创建自定义模块、配置路由、控制器和插件,可以实现自定义按钮的添加和功能的实现,并提供了完整的代码示例和配置步骤。

创建自定义模块

首先,创建一个自定义模块来实现所需的功能。按照 Magento 2 的模块结构,创建以下文件:

注册文件 (registration.php)

<?phpMagentoFrameworkComponentComponentRegistrar::register(    MagentoFrameworkComponentComponentRegistrar::MODULE,    'MG_Dropship',    __DIR__);

该文件用于注册模块。

模块配置文件 (etc/module.xml)

    

该文件定义了模块的基本信息,例如模块名称和版本。

配置依赖注入 (di.xml)

使用 di.xml 文件配置依赖注入,以便在订单详情页面添加自定义按钮。

             

这个配置将 MGDropshipPluginSalesBlockAdminhtmlOrderButton 插件应用到 MagentoSalesBlockAdminhtmlOrderView 块,允许我们修改订单详情页面的行为。

配置路由 (routes.xml)

为了处理自定义按钮的点击事件,需要配置路由。

                                    

该文件定义了一个名为 mg_dropship 的路由,其 frontName 为 mg_dropship。

创建控制器 (Controller/Adminhtml/Order/Index.php)

创建一个控制器来处理按钮点击后的逻辑。

orderRepository = $orderRepository;        $this->logger = $logger;        parent::__construct($context);    }    /**     * Execute action     *     * @throws MagentoFrameworkExceptionLocalizedException|Exception     */    public function execute()    {        $orderId = $this->getRequest()->getParam('order_id');        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);        try {            $order = $this->orderRepository->get($orderId);            // TODO: Do something with the order            $this->messageManager->addSuccessMessage(__('We did something!'));        } catch (MagentoFrameworkExceptionLocalizedException $e) {            $this->messageManager->addErrorMessage($e->getMessage());        } catch (Exception $e) {            $this->messageManager->addErrorMessage(__('We can't process your request' . $e->getMessage()));            $this->logger->critical($e);        }        return $resultRedirect->setPath(            'sales/order/view',            [                'order_id' => $orderId            ]        );    }    /**     * @return bool     */    protected function _isAllowed()    {        return $this->_authorization->isAllowed('MG_Dropship::order_dosomething');    }}

这个控制器获取订单ID,执行一些操作(TODO部分),并重定向回订单详情页面。

创建插件 (Plugin/Adminhtml/Order/Button.php)

创建一个插件来向订单详情页面添加自定义按钮。

getOrder()) {            $message = __('Are you sure you want to Do Something?');            $subject->addButton(                'do_something',                [                    'label' => __('Do Something'),                    'class' => 'do_something',                    'onclick' => "confirmSetLocation('{$message}', '{$subject->getUrl('mg_dropship/order/index', ['order_id' => $subject->getOrder()->getId()])}')"                ]            );        }    }}

这个插件使用 beforeSetLayout 方法在订单详情页面添加一个名为 “Do Something” 的按钮。点击该按钮会弹出一个确认对话框,然后重定向到 mg_dropship/order/index 路由,并传递订单ID。

清理缓存和重新部署

完成以上步骤后,需要清理 Magento 2 的缓存并重新部署静态内容。

php bin/magento cache:cleanphp bin/magento setup:upgradephp bin/magento setup:di:compilephp bin/magento setup:static-content:deploy -f

注意事项

确保模块已启用。检查文件路径和命名空间是否正确。根据实际需求修改控制器中的逻辑。如果开启了 “Add Secret Key to URLs”,请确保URL包含正确的 Form Key。

总结

通过以上步骤,你可以在 Magento 2 后台的订单详情页面成功添加一个自定义按钮,并配置其点击后的功能。这个方法可以扩展到其他页面和功能,实现更复杂的自定义需求。

以上就是Magento 2:在订单详情页添加自定义按钮并实现功能的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1291187.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月11日 07:15:35
下一篇 2025年12月11日 07:15:50

相关推荐

发表回复

登录后才能评论
关注微信