0

I want to add custom action inside mass action in Sales/Order, here the code:

sales_order_grid.xml

<?xml version="1.0" encoding="UTF-8"?>
<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../Ui/etc/ui_configuration.xsd">
  <listingToolbar name="listing_top">
    <massaction name="listing_massaction">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">
                <item name="component" xsi:type="string">Magento_Ui/js/grid/tree-massactions</item>
                <item name="indexField" xsi:type="string">id</item>
            </item>
        </argument>
        <action name="mass_shipper">
            <settings>
                <type>book_shipper</type>
                <label translate="true">Book Shipper</label>
                <actions class="Vendor\Module\Ui\Component\MassAction\MassBook\Options"/>
            </settings>
        </action>
    </massaction>
</listingToolbar>
</listing>

Options.php

<?php declare(strict_types=1);
namespace Module\Vendor\Ui\Component\MassAction\MassBook;

use Magento\Framework\Phrase;
use Magento\Framework\UrlInterface;
use Zend\Stdlib\JsonSerializable;
use Module\Vendor\Helper\Data as Helper;

class Options implements JsonSerializable
{
    protected $options;
    protected $data;
    protected $urlBuilder;
    protected $urlPath;
    protected $paramName;
    protected $additionalData = [];
    protected $helper;
 
    public function __construct(
        UrlInterface $urlBuilder,
        array $data = [],
        Helper $helper
    )
    {
        $this->data = $data;
        $this->urlBuilder = $urlBuilder;
        $this->helper = $helper;
    }
 
    public function getTime()
    {   
        $times = json_decode($this->helper->GetTimeSlot(), true);
        return $times['data']['timeSlots'];
    }
    public function jsonSerialize()
    {
        $results = $this->getTime();   
        $options = array();
        foreach($results as $result)
        {
            array_push($options, 
                $dates = [
                    'value' => $result['startTime'].'|'.$result['endTime'],
                    'label' => $result['startTime']
                ]
                );
        }
        if ($this->options === null) {
            $this->prepareData();
            foreach ($options as $optionCode) {
                $this->options[$optionCode['value']] = [
                    'type' => $optionCode['value'],
                    'label' => __($optionCode['label']),
                    '__disableTmpl' => true
                ];
 
                if ($this->urlPath && $this->paramName) {
                    $this->options[$optionCode['value']]['url'] = $this->urlBuilder->getUrl(
                        'shipperid/order/massBook/'
                    );
                }
                
                $this->options[$optionCode['value']] = array_merge_recursive(
                    $this->options[$optionCode['value']],
                    $this->additionalData
                );
            }
 
            $this->options = array_values($this->options);
        }
        return $this->options;
    }
 
    protected function prepareData()
    {
        foreach ($this->data as $key => $value) {
            switch ($key) {
                case 'urlPath':
                    $this->urlPath = $value;
                    break;
                case 'paramName':
                    $this->paramName = $value;
                    break;
                case 'confirm':
                    foreach ($value as $messageName => $message) {
                        $this->additionalData[$key][$messageName] = (string)new Phrase($message);
                    }
                    break;
                default:
                    $this->additionalData[$key] = $value;
                    break;
            }
        }
    }
}
?>

The action is worked as expected, but whenever I tried to view order details inside the grid always redirecting me to the order detail on customer side not admin side. Does anyone know why adding new action in mass action resulted changing the behaviour of the order details?

1 Answer 1

0

Pls try the below code

<listing xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Ui:etc/ui_configuration.xsd">
    <listingToolbar name="listing_top">
            <massaction name="listing_massaction">
                <action name="order_delete">
                    <argument name="data" xsi:type="array">
                        <item name="config" xsi:type="array">
                            <item name="type" xsi:type="string">add_custom_mass</item>
                            <item name="label" xsi:type="string" translate="true">Delete Order(s)</item>
                            <item name="url" xsi:type="url" path="deleteorder/deleteorder/massDelete"/>
                            <item name="confirm" xsi:type="array">
                                <item name="title" xsi:type="string" translate="true">Delete Order(s)</item>
                                <item name="message" xsi:type="string" translate="true">Are you sure you wan't to delete selected items?</item>
                            </item>
                        </item>
                    </argument>
                </action>
            </massaction>
    </listingToolbar>
</listing>
1
  • I need to send the data to my Options, ur answer didn't do that. sorry I can't accept it.
    – Wavesolid
    Commented Feb 7, 2022 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.