给后台order view页面顶部添加一个button按钮

3.49K 浏览开发笔记

给后台order view页面顶部添加一个button按钮

在开发中,经常会用到这个功能。

在后台订单详情页面加个自定义按钮,来处理一些业务逻辑。

如何添加自定义按钮呢?

需要先创建一个插件。

假设你已经有一个插件 名为: Zou_Test

分3步:

一,在di.xml里定义plugin类

vim etc/di.xml

<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nOnamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"     <type name="MagentoSalesBlockAdminhtmlOrderView">      <plugin name="addMyButton" type="ZouTestPluginBlockAdminhtmlOrderView"/>     </type> </config>

二, 编写创建button类的代码

vim PluginBlockAdminhtmlOrderView

<?php  namespace ZouTestPluginBlockAdminhtmlOrder; class View {     public function beforeSetLayout(MagentoSalesBlockAdminhtmlOrderView $subject){         $order = $subject->getOrder();         if($order->getId()){             $message = __(&#39;Are you sure you want to do this?&#39;);             $url = $subject->getUrl(&#39;testadmin/index/markAllPaid&#39;,[&#39;id&#39;=>$subject->getOrderId()]);             $subject->addButton(                 &#39;mybutton&#39;,                 [                     &#39;label&#39; => __(&#39;Mark As All Paid&#39;),                      &#39;onclick&#39; => "confirmSetLocation(&#39;{$message}&#39;, &#39;{$url}&#39;)",                     &#39;class&#39; => &#39;reset&#39;                 ],                 100             );         }              } } ?>

三,编写控制器代码

vim ControllerAdminhtmlIndexMarkAllPaid.php

<?php /**  * Copyright © Magento, Inc. All rights reserved.  * See COPYING.txt for license details.  */ namespace ZouTestControllerAdminhtmlIndex; class MarkAllPaid extends MagentoSalesControllerAdminhtmlOrder {     /**      * Authorization level of a basic admin session      *      * @see _isAllowed()      */     const ADMIN_RESOURCE = &#39;Magento_Sales::polarmoss&#39;;     /**      * Hold order      *      * @return MagentoBackendModelViewResultRedirect      */     public function execute()     {         $resultRedirect = $this->resultRedirectFactory->create();         $order = $this->_initOrder();         if ($order) {             try {                 $order->setIsHalfPaid(1);                 $order->setDueAmount(0);                 $order->save();                 //$this->orderManagement->hold($order->getEntityId());                 $this->messageManager->addSuccess(__(&#39;You order has mark as all paid.&#39;));             } catch (MagentoFrameworkExceptionLocalizedException $e) {                 $this->messageManager->addError($e->getMessage());             } catch (Exception $e) {                 $this->messageManager->addError(__(&#39;You have not mark as all paid.&#39;));             }             $resultRedirect->setPath(&#39;sales/order/view&#39;, [&#39;order_id&#39; => $order->getId()]);             return $resultRedirect;         }         $resultRedirect->setPath(&#39;sales/order/view&#39;);         return $resultRedirect;     } }

在这个控制器里对订单进行处理,完成后再跳转到订单详情页面去。

最后于 6月前 被admin编辑 ,原因:
0