添加具有订单详细信息功能的自定义按钮 - Magento 2
Posted
技术标签:
【中文标题】添加具有订单详细信息功能的自定义按钮 - Magento 2【英文标题】:Add Custom Button with Functionality to Order Detail - Magento 2 【发布时间】:2021-12-11 11:27:11 【问题描述】:我正在尝试向管理订单详细信息页面添加自定义按钮。使用下面的代码,按钮可以正确显示。但是,当单击页面时会加载到 404 页面。我似乎无法找到使其到达正确路线的路线配置。
/app/code/MG/Dropship/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'MG_Dropship',
__DIR__
);
/app/code/MG/Dropship/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="MG_Dropship" setup_version="1.0.0"/>
</config>
/app/code/MG/Dropship/etc/adminhtml/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="Magento\Backend\Block\Widget\Button\Toolbar">
<plugin name="addCustomButton" type="MG\Dropship\Plugin\Adminhtml\AddCustomButton" />
</type>
</config>
/app/code/MG/Dropship/etc/adminhtml/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="mg_dropship" frontName="mg_dropship">
<module name="MG_Dropship" />
</route>
</router>
</config>
/app/code/MG/Dropship/Controller/AdminHtml/Order/Index.php
<?php
namespace MG\Dropship\Controller\Adminhtml\Order;
class Index extends \Magento\Sales\Controller\Adminhtml\Order
/**
* Execute action
*
* @throws \Magento\Framework\Exception\LocalizedException|\Exception
*/
public function execute()
// In case you want to do something with the order
$order = $this->_initOrder();
$resultRedirect = $this->resultRedirectFactory->create();
try
// TODO: Do something with the order
$this->messageManager->addSuccessMessage(__('We did something!'));
catch (\Exception $e)
$this->messageManager->addErrorMessage(__($e->getMessage()));
return $resultRedirect->setPath('sales/order/view', [ 'order_id' => $order->getId() ]);
/**
* @return bool
*/
protected function _isAllowed()
return $this->_authorization->isAllowed('MG_Dropship::order_dosomething');
/app/code/MG/Dropship/Plugin/Adminhtml/AddCustomButton.php
<?php
namespace MG\Dropship\Plugin\Adminhtml;
class AddCustomButton
/**
* @param \Magento\Backend\Block\Widget\Button\Toolbar\Interceptor $subject
* @param \Magento\Framework\View\Element\AbstractBlock $context
* @param \Magento\Backend\Block\Widget\Button\ButtonList $buttonList
*/
public function beforePushButtons(
\Magento\Backend\Block\Widget\Button\Toolbar\Interceptor $subject,
\Magento\Framework\View\Element\AbstractBlock $context,
\Magento\Backend\Block\Widget\Button\ButtonList $buttonList
)
if ($context->getRequest()->getFullActionName() == 'sales_order_view')
$url = $context->getUrl('mg_dropship/order/index');
$buttonList->add(
'customButton',
['label' => __('Do Something'), 'onclick' => 'setLocation("' . $url . '")', 'class' => 'reset'],
-1
);
为了测试,我在 Stores > Settings > Configuration > Advanced > Admin > Security
中禁用了“将密钥添加到 URL”
如果可以启用并使用此代码,那就太好了。
非常感谢任何帮助。
【问题讨论】:
【参考方案1】:请使用它。它可以为你工作
/app/code/MG/Dropship/registration.php
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'MG_Dropship',
__DIR__
);
/app/code/MG/Dropship/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="MG_Dropship" setup_version="1.0.0"/>
</config>
/app/code/MG/Dropship/etc/adminhtml/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="Magento\Sales\Block\Adminhtml\Order\View">
<plugin name="addCustomButton" type="MG\Dropship\Plugin\Sales\Block\Adminhtml\Order\Button"/>
</type>
</config>
/app/code/MG/Dropship/etc/adminhtml/routes.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="admin">
<route id="mg_dropship" frontName="mg_dropship">
<module name="MG_Dropship" />
</route>
</router>
</config>
/app/code/MG/Dropship/Controller/AdminHtml/Order/Index.php
<?php
namespace MG\Dropship\Controller\Adminhtml\Order;
class Index extends \Magento\Sales\Controller\Adminhtml\Order
/**
* 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');
/app/code/MG/Dropship/Plugin/Adminhtml/Order/Button.php
<?php
namespace MG\Dropship\Plugin\Sales\Block\Adminhtml\Order;
class Button
public function beforeSetLayout(OrderView $subject)
if ($subject->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')')"
]
);
谢谢!
【讨论】:
以上是关于添加具有订单详细信息功能的自定义按钮 - Magento 2的主要内容,如果未能解决你的问题,请参考以下文章
在 Woocommerce 的管理订单详细信息部分显示自定义结帐字段值
根据 WooCommerce 中的自定义字段值将文本添加到订单摘要