3

I've followed this link to add custom button in Admin Sales Order View in Magento 2, and it works great. Now I want to replace this button with 3 dropdown options button. Something like changing store scope in Admin Panel. I want this 3 buttons to run same controller method with different parameters. How can i achieve this?
Thanks in advance.

SebastianT
  • 716
  • 3
  • 14

3 Answers3

2

This code works for me , try to look splitButton

class ViewPlugin
{
public function beforeSetLayout(
    MagentoView $view,
    LayoutInterface $layout
) {

    $addButtonProps = [
        'id' => 'test',
        'label' => __('Test'),
        'class' => 'add',
        'button_class' => '',
        'class_name' => 'Magento\Backend\Block\Widget\Button\SplitButton',
        'options' => $this->getCustomActionListOptions(),
    ];
    $view->addButton('test',$addButtonProps);
}

protected function getCustomActionListOptions()
{
    /*list of button which you want to add*/
    $splitButtonOptions=[
        'action_1'=>['label'=>__('Action 1'),'onclick'=>'setLocation("ACTION 1")'],
        'action_2'=>['label'=>__('Action 2'),'onclick'=>'setLocation("ACTION 2")'],
        'action_3'=>['label'=>__('Action 3'),'onclick'=>'setLocation("ACTION 3")']
    ];

    return $splitButtonOptions;
}

}

 <type name="Magento\Sales\Block\Adminhtml\Order\View">
    <plugin name="addMyButton" type="My\Module\Plugin\Block\Adminhtml\Order\View"/>
   </type>
Kai
  • 146
  • 8
  • Thanks for your answer. Today or tomorrow i will try and let you know :) – SebastianT Jul 16 '20 at 07:30
  • after i wrote above comment i realized that it uses the same plugin. So i tested and it works :D thanks, and also thanks for information that this is split button :D – SebastianT Jul 16 '20 at 07:38
0

I have created it by calling sales_order_view.xml first create sales_order_view.xml in vendor/module/view/adminhtml/layout/sales_order_view.xml

-1
namespace vendor\module\Block\Adminhtml\Order\View;

class Buttons extends \Magento\Sales\Block\Adminhtml\Order\View
{    

   protected function _construct()
    {
        parent::_construct();

        if(!$this->getOrderId()) {
            return $this;
        }      

        $addButtonProps = [
            'id' => 'stocksheet',
            'label' => __('Shocksheet'),
            'class' => 'add',
            'button_class' => '',
            'class_name' => 'Magento\Backend\Block\Widget\Button\SplitButton',
            'options' => $this->getCustomActionListOptions(),
        ];
        $this->addButton('test',$addButtonProps);

        return $this;
    }

    protected function getCustomActionListOptions()
    {
        $csvUrl = $this->_urlBuilder->getUrl(
            'vestocksheet/index',
            ['order_id' => $this->getOrder()->getIncrementId()]
        );

        /*list of button which you want to add*/
        $splitButtonOptions=[
            'csv'=>['label'=>__('CSV'),'onclick' => 'setLocation(\'' . $csvUrl . '\')'],
            'pdf'=>['label'=>__('PDF'),'onclick' => 'setLocation(\'' . $csvUrl . '\')']
        ];
        return $splitButtonOptions;
    }

}
Deep Shah
  • 565
  • 5
  • 31