0

我正在尝试将自定义按钮添加到管理订单详细信息页面。使用下面的代码,按钮可以正确显示。但是,当单击页面时会加载到 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

如果可以启用并使用此代码,那就太好了。

任何帮助是极大的赞赏。

4

1 回答 1

0

请使用它。它可以为你工作

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

谢谢你!

于 2021-11-25T06:22:41.563 回答