登录
首页 >  文章 >  php教程

Magento2订单详情页添加自定义按钮教程

时间:2025-08-11 21:48:54 108浏览 收藏

还在为Magento 2后台订单详情页的功能扩展发愁吗?本文档提供了一份详尽的教程,手把手教你如何在Magento 2的订单详情页面添加自定义按钮,并实现特定的业务逻辑。我们将深入探讨模块的创建与配置,包括注册模块、声明模块、修改di.xml配置插件、配置路由以及创建控制器等关键步骤。通过使用插件(Plugin)修改Magento\Sales\Block\Adminhtml\Order\View块的行为,轻松实现按钮添加。同时,详细讲解控制器编写,确保按钮点击后的业务逻辑能够正确执行,并提供URL安全密钥兼容方案,保障链接安全。最后,别忘了清理缓存并更新模块,让你的自定义按钮功能完美上线!

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

本文档旨在指导开发者如何在 Magento 2 的后台订单详情页面添加一个自定义按钮,并在点击该按钮后执行特定的业务逻辑。我们将详细介绍模块的创建、配置、控制器编写以及插件的使用,确保按钮功能正常运作,并兼容 URL 安全密钥。

模块创建与配置

首先,我们需要创建一个自定义模块来实现该功能。按照 Magento 2 的模块开发规范,创建以下目录和文件:

  1. 注册模块 (registration.php)

    在 app/code/MG/Dropship/registration.php 文件中注册模块:

  2. 模块声明 (module.xml)

    在 app/code/MG/Dropship/etc/module.xml 文件中声明模块:

    
    
        
    

修改 di.xml

在 app/code/MG/Dropship/etc/adminhtml/di.xml 中,我们使用插件(Plugin)来修改 Magento\Sales\Block\Adminhtml\Order\View 块的行为,以便添加自定义按钮。



   
       
   

配置路由 (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 \Magento\Framework\Exception\LocalizedException|\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 (\Magento\Framework\Exception\LocalizedException $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');
    }
}

此控制器继承自 \Magento\Sales\Controller\Adminhtml\Order,并重写了 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 方法,在布局渲染之前向 Magento\Sales\Block\Adminhtml\Order\View 块添加一个名为 do_something 的按钮。

  • label: 按钮上显示的文本。
  • class: 按钮的 CSS 类,可以自定义样式。
  • onclick: 按钮点击时执行的 JavaScript 代码,这里使用 confirmSetLocation 函数显示一个确认对话框,然后重定向到 mg_dropship/order/index 路由。

清理缓存并更新模块

完成以上步骤后,需要清理 Magento 2 的缓存并更新模块:

  1. 执行 php bin/magento setup:upgrade 命令。
  2. 执行 php bin/magento setup:di:compile 命令。
  3. 执行 php bin/magento setup:static-content:deploy -f 命令。
  4. 执行 php bin/magento cache:clean 命令。
  5. 执行 php bin/magento cache:flush 命令。

总结

通过以上步骤,我们成功地向 Magento 2 的后台订单详情页添加了一个自定义按钮,并在点击该按钮后执行了特定的业务逻辑。 此方案兼容 URL 安全密钥,确保了链接的安全性。 在实际开发中,你需要根据具体的业务需求修改控制器中的逻辑和按钮的样式。

到这里,我们也就讲完了《Magento2订单详情页添加自定义按钮教程》的内容了。个人认为,基础知识的学习和巩固,是为了更好的将其运用到项目中,欢迎关注golang学习网公众号,带你了解更多关于的知识点!

相关阅读
更多>
最新阅读
更多>
课程推荐
更多>