首页 文章

在Magento 2 Admin的自定义模块中获取Order_ID

提问于
浏览
0

有人可以帮我在自定义模块的onclick-setLocation中编写orderID参数吗?我正在尝试将 a button with custom URL 添加到管理>销售>订单 - >查看页面 .

请检查下面的代码 . 我只想写我的按钮的onclick功能就像 external_link.php?id=8 .

<?php

namespace Myself\AdminInvoiceColumn\Plugin\Block\Widget\Button;

use Magento\Backend\Block\Widget\Button\Toolbar as ToolbarContext;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Backend\Block\Widget\Button\ButtonList;

class Toolbar
{
/**
 * @param ToolbarContext $toolbar
 * @param AbstractBlock $context
 * @param ButtonList $buttonList
 * @return array
 */



public function beforePushButtons(
    ToolbarContext $toolbar,
    \Magento\Framework\View\Element\AbstractBlock $context,
    \Magento\Backend\Block\Widget\Button\ButtonList $buttonList
) { 
    if (!$context instanceof \Magento\Sales\Block\Adminhtml\Order\View) {
        return [$context, $buttonList];
    } 
    $buttonList->update('order_edit', 'class', 'edit');
    $buttonList->update('order_invoice', 'class', 'invoice primary');
    $buttonList->update('order_invoice', 'sort_order', (count($buttonList->getItems()) + 1) * 10);

    $buttonList->add('order_review',
        [
            'label' => __('Custom Button'),
            'onclick' => 'setLocation(\'/external_link.php?'.$id.'\')',
            'class' => 'review'
        ]
    );


    return [$context, $buttonList];
}
}

1 回答

  • 0

    在我看来,创建自定义按钮以及添加订单ID以创建自定义URL的最简单和最好的方法是创建 setLayout 插件功能 .

    首先,您必须在 Custom_Vendor/Custom_Module/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="custom_button" type="Custom_Vendor\Custom_Module\Plugin\Sales\Block\Adminhtml\Order\View"/>
        </type>
    </config>
    

    然后,扩展 before 函数,如下所示:

    <?php
    namespace Custom_Vendor\Custom_Module\Plugin\Sales\Block\Adminhtml\Order;
    
    use Magento\Sales\Block\Adminhtml\Order\View as OrderView;
    
    class View
    {
        public function beforeSetLayout(OrderView $subject)
        {
            $orderId = $subject->getOrderId();
    
            /**
             * Change url as per your need and you could also 
             * use $subject->getUrl('module/controller/action')
             */
            $url = '/external_link.php?' . $orderId;
    
            $subject->addButton(
                'order_custom_button',
                [
                    'label' => __('Custom Button'),
                    'class' => 'review',
                    'onclick' => "setLocation('{$url}')"
                ]
            );
        }
    }
    

相关问题