Get Free Audit

How to Add Custom Mass Action in Order Grid in Magento 2

Oct 25, 2018

8190 Andrey Litvin

How to Add Custom Mass Action in Order Grid in Magento 2

One of our clients needed to add a custom order status “in preparation” and to send an email along with changing to this status. Moreover, the client’s requirement included the ability to change the status of the order from the orders grid using the mass action.

To solve the problem you will need to:

  1. create and configure new order status;
  2. add email template and settings for it;
  3. add a mass action to the orders grid;
  4. register an order change event and send an email when the order status is changed.

Ok, let’s start.

Creating a new order status

Open the Magento 2 admin and go to Stores ➜ Settings ➜ Order Status. Click Create New Status.

How to Add Custom Mass Action in Order Grid in Magento 2

Fill in the form. In our case, a preparation status is created with the “In preparation” label. Click Save Status.

How to Add Custom Mass Action in Order Grid in Magento 2

After that click the Assign Status to State button and assign the “In preparation” status to the “Processing” state.

Step 1 is completed. Well done!

To perform the following steps, you need to create a module:

The symbol “$>” will indicate the actions performed in the bash console.

$> mkdir -p app/code/Belvg/OrderCustomStatus/etc

$> cd app/code/Belvg/OrderCustomStatus

Next come all the paths relative to the root of the module pp/code/Belvg/OrderCustomStatus. The content of the file follows Immediately after the “touch” command.

$> touch etc/module.xml


$> touch registration.php


These are the basic files of the empty module.

Next, you need to store the “Status Code” of the created status somewhere, so as to use a constant in the code, and not a string.

$> mkdir Model

$> touch Model/Order.php

Adding an email template and its setting

The email template itself is declared in the email_templates.xml file.

$> touch etc/email_templates.xml


We announced a new email template with an identifier “sales_email_order_template_preparation” – it will be used in the code when generating an email, label is displayed in the email templates list in Marketing ➜ Communication ➜ Email templates;

file=”order_preparation.html”, module=”Belvg_OrderCustomStatus”, area=”frontend”, module=”Belvg_OrderCustomStatus” indicate that the file with the content of a template is located in the folder of the “Belvg_OrderCustomStatus” module (this is our module) in the view/frontend/email folder. From here Magento will try to download a template when creating a template in Marketing ➜ Communication ➜ Email templates. This is defined in xml. since the place can be in adminhtml as well. Everything depends on the specified area attribute.

Let’s create this file:

$> mkdir -p view/frontend/email/

$> touch view/frontend/email/order_preparation.html


Here you must specify “@subject”. From here Magento will try to get the subject of the email by default.

Create a setting in Stores ➜ Settings ➜ Configuration ➜ Sales ➜ Sales Emails ➜ Order ➜ Order Preparation Template, so that you can override the email template for different stores.

$> mkdir -p etc/adminhtml

$> touch etc/adminhtml/system.xml

Add a mass action to the orders grid

The view/adminhtml/ui_component/sales_order_grid.xml file is responsible for adding a mass action.

Let’s create it:

$> mkdir -p view/adminhtml/ui_component/

$> touch view/adminhtml/ui_component/sales_order_grid.xml


An action is created here named “Orders processed”. If it is selected, all the marked orders will be sent to the URL <admin_backend>/ordercustomstatus/order/preparation.

First of all, in order to declare the base URL of our module, Magento will send all requests of the <admin_backend>/ordercustomstatus/* form to our module $> touch etc/adminhtml/routes.xml.


Now we create a controller, which will process the query from the mass action:

$> mkdir -p Controller/Adminhtml/Order

$> touch Preparation.php

Igor Dragun
Partner With Us Let's discuss how to grow your business. Get a Free Quote.
Talk to Igor


In this controller, the control is transferred to the execute method, in which orders from the collection are selected one by one. It is also checked that the state of the order is STATE_NEW, its status is changed and gets written to the database. It is important to use \Magento\Sales\Api\OrderRepositoryInterface, otherwise the order change event will not work.

Registering an order change event and sending an email when the order status is changed

Create an events.xml file:

$> touch etc/events.xml


and its observer:

$> mkdir Observer

$> touch OrderStatusPreparationObserver.php


When saving the order, if the model has been changed, Magento will call the event handlers of the sales_order_save_after event (in fact, there will be much more handlers but for now we are only interested in this one) and will transfer control to the execute method of our observer. In the method, it is checked if the order status has become STATUS_PREPARATION. After that, an email is generated, the storeId received from the order is indicated.

Done! Now, when the status of the order is changed to preparation, the buyer will be notified about it by email. And it doesn’t matter where the change was made: in the list of orders (orders grid) or in the order editing page.

It remains to enable our module with the following command:

$> php bin/magento setup:upgrade

That is how you add a custom mass action in order grid in Magento 2, I hope it was clear and helpful.

Extensions for Magento 2

Extensions for Magento 2

Take your online store to the new level

Download here
Igor Dragun
Partner With Us Looking for a partner to grow your business? We are the right company to bring your webstore to success. Talk to Igor

1 comment

Post a new comment

BelVG Newsletter
Subscribe to our mailing list and get interesting stuff and updates to your email inbox.
Email *