TransWikia.com

Using Observer stop execution of next function in Magento 2

Magento Asked by Nausif on October 2, 2021

I am using an observer on event sales_order_save_before to check for a condition and based on that condition I was to stop the save functionality.

Currently I am using it on Submit Comment in Sales Order view (Admin Panel)

My Observer execute function is called and condition is met. but it doesn’t stop the save functionality when I raise error using throw new ErrorException()

Is there any other way to completely stop the save order process?

One Answer

This is what I learnt from magento 2 certification exam : the data that is sent to events should not be transformed. Events should be able to be run completely asynchronously, so around plugin can be used to stop logic. For Submit Comment create aroundExecute() for MagentoSalesControllerAdminhtmlOrderAddComment

public function aroundExecute(AddComment $subject, Closure $proceed){
    //your statement;
    if (your statement) {
       proceed();
    }
    else{
       // stop original chain
       //adderrorMassage();
       redirect or throw Error;
       return;
    }

}

Correct answer by Kai on October 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP