
本文档详细介绍了如何在 Magento 2 后台的订单详情页面添加一个自定义按钮,并实现点击后触发特定功能。通过本文,你将学习如何创建模块、配置路由、编写控制器和插件,最终实现自定义按钮的功能。同时,本文也考虑了启用 “Add Secret Key to URLs” 的情况,提供更全面的解决方案。
创建模块
首先,我们需要创建一个自定义模块。按照 Magento 2 的模块结构,创建以下文件:
注册文件 (registration.php)
<?phpMagentoFrameworkComponentComponentRegistrar::register( MagentoFrameworkComponentComponentRegistrar::MODULE, 'MG_Dropship', __DIR__);
这个文件告诉 Magento 2 注册名为 MG_Dropship 的模块。
模块配置文件 (etc/module.xml)
这个文件声明了模块的名称和版本。
配置依赖注入 (DI)
接下来,我们需要通过 di.xml 文件配置依赖注入,以便在订单详情页添加自定义按钮。
这个配置使用了一个插件(MGDropshipPluginSalesBlockAdminhtmlOrderButton)来拦截 MagentoSalesBlockAdminhtmlOrderView 类的 beforeSetLayout 方法,从而可以在订单详情页添加自定义按钮。
配置路由
我们需要定义一个路由,以便处理自定义按钮的点击事件。
这个文件定义了一个名为 mg_dropship 的 admin 路由,frontName 也设置为 mg_dropship。
创建控制器
我们需要创建一个控制器来处理自定义按钮的点击事件。
logger = $logger; parent::__construct($context, $coreRegistry, $fileFactory, $orderSender, $resultForwardFactory, $resultPageFactory); } /** * Execute action * * @throws MagentoFrameworkExceptionLocalizedException|Exception */ public function execute() { // In case you want to do something with the order $order = $this->_initOrder(); if ($order) { try { // 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 $this->resultRedirectFactory->create()->setPath( 'sales/order/view', [ 'order_id' => $order->getEntityId() ] ); } return $this->resultRedirectFactory->create()->setPath('sales/*/'); } /** * @return bool */ protected function _isAllowed() { return $this->_authorization->isAllowed('MG_Dropship::order_dosomething'); }}
这个控制器继承自 MagentoSalesControllerAdminhtmlOrder,并重写了 execute 方法。在 execute 方法中,你可以编写自定义逻辑来处理订单。 关键是使用 $this->_initOrder() 来初始化订单对象。同时,需要实现 _isAllowed 方法来控制访问权限。
创建插件
我们需要创建一个插件来向订单详情页添加自定义按钮。
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')}')" ] ); } }}
这个插件拦截了 MagentoSalesBlockAdminhtmlOrderView 类的 beforeSetLayout 方法,并使用 $subject->addButton 方法添加了一个名为 do_something 的按钮。onclick 属性定义了点击按钮后执行的 JavaScript 代码,它会弹出一个确认框,并在确认后跳转到 mg_dropship/order/index 路由。
定义权限
为了控制用户访问自定义功能的权限,需要在 etc/adminhtml/acl.xml 文件中定义权限规则。
这个文件定义了一个名为 MG_Dropship::order_dosomething 的权限规则,你需要在后台配置用户角色,授予他们这个权限才能访问自定义功能。
启用 “Add Secret Key to URLs” 的情况
当启用 “Add Secret Key to URLs” 时,Magento 会自动在 URL 中添加一个 secret key,以增加安全性。 以上代码已经兼容这种情况,$subject->getUrl(‘mg_dropship/order/index’) 会自动生成包含 secret key 的 URL。
总结
通过以上步骤,你就可以在 Magento 2 后台的订单详情页添加一个自定义按钮,并实现点击后触发特定功能。请确保在修改代码后清除缓存,并检查是否正确配置了权限。
以上就是Magento 2:向订单详情页添加自定义功能按钮的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1268489.html
微信扫一扫
支付宝扫一扫