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

时间:2018-05-16 01:38:43

标签: php magento magento2

有人可以帮我在自定义模块的onclick-setLocation中编写orderID参数吗?我正在尝试将带有自定义网址的按钮添加到 admin> sales> order-> view 页面。

请检查下面的代码。我只想写我的按钮的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 :(得分:1)

在我看来,创建自定义按钮以及添加订单ID以制作自定义网址的最简单方法是创建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}')"
            ]
        );
    }
}