添加自定义按钮与功能的订单详细信息- Magento 2



我试图添加一个自定义按钮到管理订单详细信息页面。下面的代码按钮显示正确。但是,当点击页面时,页面会加载到404页面。我似乎找不到一个路由配置,使它到达正确的路由。

/app/代码/毫克/运输船registration.php

<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'MG_Dropship',
__DIR__
);

/app/代码/毫克/运输船/etc/包含

<?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/代码/毫克/运输船/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="MagentoBackendBlockWidgetButtonToolbar">
<plugin name="addCustomButton" type="MGDropshipPluginAdminhtmlAddCustomButton" />
</type>
</config>

/app/代码/毫克/运输船/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/代码/毫克/运输船/控制器/AdminHtml/订单/index . php

<?php
namespace MGDropshipControllerAdminhtmlOrder;

class Index extends MagentoSalesControllerAdminhtmlOrder
{
/**
* Execute action
*
* @throws MagentoFrameworkExceptionLocalizedException|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/代码/毫克/运输船/插件/Adminhtml/AddCustomButton.php

<?php
namespace MGDropshipPluginAdminhtml;

class AddCustomButton
{
/**
* @param MagentoBackendBlockWidgetButtonToolbarInterceptor $subject
* @param MagentoFrameworkViewElementAbstractBlock $context
* @param MagentoBackendBlockWidgetButtonButtonList $buttonList
*/
public function beforePushButtons(
MagentoBackendBlockWidgetButtonToolbarInterceptor $subject,
MagentoFrameworkViewElementAbstractBlock $context,
MagentoBackendBlockWidgetButtonButtonList $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">

如果可以启用该功能并与此代码一起工作就太好了。

感谢您的帮助。

请使用。它可以为你工作

/app/代码/毫克/运输船registration.php

<?php
MagentoFrameworkComponentComponentRegistrar::register(
MagentoFrameworkComponentComponentRegistrar::MODULE,
'MG_Dropship',
__DIR__
);

/app/代码/毫克/运输船/etc/包含

<?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/代码/毫克/运输船/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="MagentoSalesBlockAdminhtmlOrderView">
<plugin name="addCustomButton" type="MGDropshipPluginSalesBlockAdminhtmlOrderButton"/>
</type>
</config>

/app/代码/毫克/运输船/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/代码/毫克/运输船/控制器/AdminHtml/订单/index . php

<?php
namespace MGDropshipControllerAdminhtmlOrder;

class Index extends MagentoSalesControllerAdminhtmlOrder
{
/**
* 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');
}
}

/app/代码/毫克/运输船/插件/Adminhtml/订单/Button.php

<?php
namespace MGDropshipPluginSalesBlockAdminhtmlOrder;

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')}')"
]
);
}
}
}

谢谢!

相关内容

  • 没有找到相关文章

最新更新