
本文档旨在指导开发者如何在 Magento 2 的后台订单详情页面添加一个自定义按钮,并在点击该按钮后执行特定的业务逻辑。我们将详细介绍模块的创建、配置、控制器编写以及插件的使用,确保按钮功能正常运作,并兼容 URL 安全密钥。
模块创建与配置
首先,我们需要创建一个自定义模块来实现该功能。按照 Magento 2 的模块开发规范,创建以下目录和文件:
注册模块 (registration.php)
在 app/code/MG/Dropship/registration.php 文件中注册模块:
<?phpMagentoFrameworkComponentComponentRegistrar::register( MagentoFrameworkComponentComponentRegistrar::MODULE, 'MG_Dropship', __DIR__);
模块声明 (module.xml)
在 app/code/MG/Dropship/etc/module.xml 文件中声明模块:
修改 di.xml
在 app/code/MG/Dropship/etc/adminhtml/di.xml 中,我们使用插件(Plugin)来修改 MagentoSalesBlockAdminhtmlOrderView 块的行为,以便添加自定义按钮。
配置路由 (routes.xml)
在 app/code/MG/Dropship/etc/adminhtml/routes.xml 文件中定义后台路由:
创建控制器 (Index.php)
在 app/code/MG/Dropship/Controller/AdminHtml/Order/Index.php 文件中创建控制器,用于处理按钮点击后的逻辑:
logger = $logger; parent::__construct( $context, $coreRegistry, $fileFactory, $dateFilter, $orderSender, $creditmemoSender, $invoiceSender, $shipmentSender, $resultPageFactory, $resultLayoutFactory, $resultRawFactory, $resultJsonFactory, $messageManager ); } /** * 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() 方法。在该方法中,首先初始化订单,然后执行自定义逻辑(TODO 部分),最后重定向回订单详情页。
注意:
请务必替换 // TODO: Do something with the order 部分,实现你的具体业务逻辑。_isAllowed() 方法用于权限控制,确保只有具有 MG_Dropship::order_dosomething 权限的用户才能访问该功能。
创建插件 (Button.php)
在 app/code/MG/Dropship/Plugin/Sales/Block/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')}')" ] ); } }}
此插件使用 beforeSetLayout 方法,在布局渲染之前向 MagentoSalesBlockAdminhtmlOrderView 块添加一个名为 do_something 的按钮。
label: 按钮上显示的文本。class: 按钮的 CSS 类,可以自定义样式。onclick: 按钮点击时执行的 JavaScript 代码,这里使用 confirmSetLocation 函数显示一个确认对话框,然后重定向到 mg_dropship/order/index 路由。
清理缓存并更新模块
完成以上步骤后,需要清理 Magento 2 的缓存并更新模块:
执行 php bin/magento setup:upgrade 命令。执行 php bin/magento setup:di:compile 命令。执行 php bin/magento setup:static-content:deploy -f 命令。执行 php bin/magento cache:clean 命令。执行 php bin/magento cache:flush 命令。
总结
通过以上步骤,我们成功地向 Magento 2 的后台订单详情页添加了一个自定义按钮,并在点击该按钮后执行了特定的业务逻辑。 此方案兼容 URL 安全密钥,确保了链接的安全性。 在实际开发中,你需要根据具体的业务需求修改控制器中的逻辑和按钮的样式。
以上就是Magento 2:向订单详情页添加自定义按钮及功能实现的详细内容,更多请关注创想鸟其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1268493.html
微信扫一扫
支付宝扫一扫